【问题标题】:Oracle Daily count/average over a yearOracle 每日计数/一年内的平均值
【发布时间】:2012-07-19 19:24:43
【问题描述】:

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

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

【问题讨论】:

    标签: oracle count average


    【解决方案1】:

    似乎在第一个你想将组更改为当天 - 而不是时间......(另外我认为你不需要指定所有这些 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') ;
    

    【讨论】:

      【解决方案2】:

      不是 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)

      【讨论】:

        猜你喜欢
        • 2021-12-18
        • 2018-07-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-05
        • 1970-01-01
        • 1970-01-01
        • 2020-07-05
        • 1970-01-01
        相关资源
        最近更新 更多