Oracle Daily计算/平均一年多

时间:2012-07-19 19:24:43

标签: oracle count average

我在特定时间段内提取了两条信息,但我想获取一个标记的每日平均值和另一个标记的每日计数。我不确定如何在特定时间段内做每日平均值,有人可以提供一些建议吗?下面是我关于如何处理这个问题的第一个想法,但改变每个日期会很烦人。感谢任何帮助

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 :(得分: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') ;

答案 1 :(得分: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)

- 如果您希望查询提供该范围的总计,您可以使用汇总(http://psoug.org/reference/rollup.html