Oracle按不同时间分组统计

1、按年

select to_char(record_date,'yyyy'), sum(col_8) as total_money 
from table_name
where group by to_char(record_date,'yyyy')

2、按月

select to_char(record_date,'yyyy-mm'), sum(col_8) as total_money 
from table_name
where group by to_char(record_date,'yyyy-mm')

3、按季

select to_char(record_date,'yyyy-Q'), sum(col_8) as total_money 
from table_name
where group by to_char(record_date,'yyyy-Q')

4、按周

select to_char(record_date,'yyyy-IW'), sum(col_8) as total_money 
from table_name
where group by to_char(record_date,'yyyy-IW')

5、按小时

select to_char(record_date,'yyyy-mm-dd hh24'), sum(col_8) as total_money 
from table_name
where group by to_char(record_date,'yyyy-mm-dd hh24')

6、按分钟

select to_char(record_date,'yyyy-mm-dd hh24:mi'), sum(col_8) as total_money 
from table_name
where group by to_char(record_date,'yyyy-mm-dd hh24:mi')


SELECT trunc(sysdate-1) + (ROWNUM - 1) / 24 / 60 AS STAT_TIME
FROM DUAL d
CONNECT BY ROWNUM <= 1440 +(trunc(sysdate, 'MI') - (trunc(sysdate))) * 24 * 60

 7、按5分钟

SELECT trunc(sysdate) + (ROWNUM - 1) / 24/12 AS STAT_TIME
    FROM DUAL
    CONNECT BY ROWNUM <=(trunc(sysdate, 'MI') - (trunc(sysdate))) * 24 * 12
按5分钟统计,中间没数据的时间会缺少
select to_char(j.record_date,'yyyymmdd') as st_date
       , sum(j.col_8*j.col_9*j.col_10) as mer_value
       , to_char(j.record_date,'hh24') as STAT_HOUR
       , floor(to_char(j.record_date,'mi')/5) as min_flag
       , '昨日' as report_name     
    from c ,  j
    where c.col_4 = j.col_4 and c.col_5 = j.col_5 and c.col_6 = j.col_6
    and j.actiontype = 217
    and j.record_date between trunc(sysdate-1) and trunc(sysdate)
    group by to_char(j.record_date,'yyyymmdd'), to_char(j.record_date,'hh24'), floor(to_char(j.record_date,'mi')/5)
ORDER BY st_date, STAT_HOUR, min_flag

 

select STAT_TIME tdate,
  'test' app_id,
  goods_type, --c.col_4 as
  goods_id, --c.col_5 as
  goods_currency_type --c.col_6 as
  from
  (
    SELECT trunc(sysdate-1) + (ROWNUM - 1) / 24 / 12 AS STAT_TIME
    FROM DUAL d
    CONNECT BY ROWNUM <= 288 +(trunc(sysdate, 'MI') - (trunc(sysdate))) * 24 * 12 + 1
  ),
  (
    select
    t.goods_type,t.goods_id,t.goods_currency_type
    from mid_minutes t
    where tdate >= trunc(sysdate) -1
    and app_id = 'test'
    group by t.goods_type,t.goods_id,t.goods_currency_type
  ) c

 

8、按十分钟

SELECT trunc(sysdate) + (ROWNUM - 1) / 24/6 AS STAT_TIME
    FROM DUAL
    CONNECT BY ROWNUM <=(trunc(sysdate, 'MI') - (trunc(sysdate))) * 24 * 6

 

按5分钟统计,把时间转换成分钟准点函数

create or replace function trunc_minute(v_date date) return date as

begin
      return to_number(trunc(to_char(v_date, 'mi')/5))*5/(24*60) + trunc(v_date, 'hh24');
end;

 

9、按半小时

select to_date(to_char(j.tdate, 'yyyy-mm-dd hh24:') || case
                           when to_char(j.tdate, 'mi') < '30' then
                            '00'
                           else
                            '30'
                         end,
                         'yyyy-mm-dd hh24:mi') as tdate,                
                 j.goods_id,
                 sum(j.buy_count) as buy_count,
                 sum(j.total_price) as total_price
            from tmp_goods_mis j
             where j.tdate >= trunc(sysdate) - 1
           group by j.goods_id
                    to_char(j.tdate, 'yyyy-mm-dd hh24:') || case
                      when to_char(j.tdate, 'mi') < '30' then
                       '00'
                      else
                       '30'                      
                    end

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\],可以使用Oracle的语法来按时间段分组统计数据。首先,可以使用CONNECT BY LEVEL语句生成一个时间序列,每个时间间隔为6小时。然后,可以使用TO_CHAR函数将时间格式化为所需的格式,并使用FLOOR函数将分钟舍入到最接近的15分钟。接下来,可以使用WHERE子句筛选出指定时间范围内的数据,并使用GROUP BY子句按时间段进行分组。最后,可以使用ORDER BY子句按时间排序结果。 以下是一个示例查询的代码: ```sql SELECT TO_CHAR(StartTime, 'YYYY-MM-DD HH24:MI:SS') AS StartTime, SUM(Count) AS Counts FROM table WHERE StartTime <= TO_DATE('2016-11-29 18:00:00', 'YYYY-MM-DD HH24:MI:SS') GROUP BY TO_CHAR(StartTime, 'YYYY-MM-DD HH24:MI:SS') ORDER BY StartTime; ``` 这个查询将按照每个时间段(每15分钟)统计数据,并按照时间顺序进行排序。你可以根据自己的需求修改查询中的时间范围和时间格式。 #### 引用[.reference_title] - *1* [Oracle时间段分组统计](https://blog.csdn.net/weixin_39752157/article/details/116335359)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Oracle数据库oracle时间段统计15分钟内的数据](https://blog.csdn.net/weixin_36232899/article/details/116293382)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值