三個引數開始時間、結束時間、時間間隔
需求是:在開始時間和結束時間內,按照指定的時間間隔進行一個分組。例如開始時間為2020-07-10 08:00:00
結束時間為2020-07-11 08:00:00 時間間隔為30分鐘
分為2020-07-10 08:00:00 - 2020-07-10 08:30:00 依次類推到最后為2020-07-11 07:30:00 - 2020-07-11 08:00:00 如何寫sql陳述句。有大佬能幫忙下嗎
uj5u.com熱心網友回復:
select ceil((to_date('2020-07-11 09:30:00','yyyy-mm-dd hh24:mi:ss')-開始時間)*24*60/30)from tab_name
where ...;
uj5u.com熱心網友回復:
大兄弟不對呀uj5u.com熱心網友回復:
出不來大兄弟uj5u.com熱心網友回復:
怎么出不來,報什么錯了?uj5u.com熱心網友回復:
大胸弟,試試下面的方法把時間規格化再group by--令日期型欄位X,如果是文本就自己to_date
select decode(sign(X-trunc(X,'hh')-30/1440),-1,trunc(X,'hh')+30/1440),round(X,'hh')) as 時段 from table where X >= 開始時間 and X <= 結束時間
uj5u.com熱心網友回復:
括號每打對,編輯器自己加了括弧--令日期型欄位X,如果是文本就自己to_date
select decode(sign(X-trunc(X,'hh')-30/1440),-1,trunc(X,'hh')+30/1440,round(X,'hh')) as 時段 from table where X >= 開始時間 and X <= 結束時間
uj5u.com熱心網友回復:
with tab1 as (
select level g_id, to_date('2020071108', 'yyyymmddhh24') + (level - 1) * 1 / 24 / 2 dt1, to_date('2020071108', 'yyyymmddhh24') + (level) * 1 / 24 / 2 dt2 from dual connect by level <= floor((to_date('2020071108', 'yyyymmddhh24') - to_date('2020071008', 'yyyymmddhh24')) / (86400 / (30 * 60))) + 2
)
select * from tab t1, tab1 t2
where t1._date between t2.dt1 and t2.dt2
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/8467.html
標籤:開發
