0

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

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')
4

2 回答 2

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') ;
于 2012-07-19T19:34:57.180 回答
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

于 2012-07-19T20:00:06.853 回答