2012-07-19 41 views
0

我在特定的時間段內拉取兩條信息,但我想獲取一個標記的日平均值和另一個標記的每日計數。我不確定如何在特定的時間段內進行日常平均,任何人都可以提供一些建議?下面是我如何處理這個問題的第一個想法,但改變每一個日期都會很煩人。任何幫助表示讚賞感謝Oracle每日統計/一年以上的平均值

SELECT COUNT(distinct chargeno), to_char(chargetime, 'mmddyyyy') AS chargeend 
FROM batch_index WHERE plant=1 AND chargetime>to_date('2012-06-18:00:00:00','yyyy-mm-dd:hh24:mi:ss') 
AND chargetime<to_date('2012-07-19:00:00:00','yyyy-mm-dd:hh24:mi:ss') 
group by chargetime; 

The working version of the daily sum 
SELECT to_char(bi.chargetime, 'mmddyyyy') as chargtime, SUM(cv.val)*0.0005 
FROM Charge_Value cv, batch_index bi WHERE cv.ValueID =97 
AND bi.chargetime<=to_date('2012-07-19','yyyy-mm-dd') 
AND bi.chargeno = cv.chargeno AND bi.typ=1 
group by to_char(bi.chargetime, 'mmddyyyy') 

回答

2

好像在想的組更改爲當天的第一個 - 不是時候......(加上我不認爲你需要指定所有那些0的幾秒鐘..)

SELECT COUNT(distinct chargeno), to_char(chargetime, 'mmddyyyy') AS chargeend 
FROM batch_index WHERE plant=1 AND chargetime>to_date('2012-06-18','yyyy-mm-dd') 
AND chargetime<to_date('2012-07-19','yyyy-mm-dd') 
group by to_char(chargetime, 'mmddyyyy') ; 
0

不是100%我下面你的問題,但如果你只想做集合體(總和,平均),然後做到這一點。我在彙總扔以防萬一這就是你要找的

with fakeData as(
    select trunc(level *.66667) nr 
     , trunc(2*level * .33478) lvl --these truncs just make the doubles ints 
     ,trunc(sysdate+trunc(level*.263784123)) dte --note the trunc, this gets rid of the to_char to drop the time 
    from dual 
connect by level < 600 
) --the cte is just to create fake data 
--below is just some aggregates that may help you 
select sum(nr) daily_sum_of_nr 
    , avg(nr) daily_avg_of_nr 
    , count(distinct lvl) distinct_lvls_per_day 
    , count(lvl) count_of_nonNull_lvls_per_day 
    , dte days 
    from fakeData 
group by rollup(dte) 

--IF您想查詢提供一個總的範圍內,你可以使用匯總(http://psoug.org/reference/rollup.html