如何修改此脚本以每3分钟计数一次?

时间:2018-11-21 05:59:17

标签: sql oracle

我需要修改此查询以每3分钟间隔(即09:00-09:02和09:03-09:06)获取结果

以下查询给出每1分钟的输出计数:

select TO_CHAR(ECH.CH_VALIDFROM,'mm/dd/yyyy hh24:mi'), count(1) count
from emcdu.ext_contract_history ech
where ch_status='a' and CH_VALIDFROM BETWEEN TO_DATE('21/11/2018 
09:00:00','dd/mm/yyyy hh24:mi:ss') 
AND TO_DATE ('21/11/2018 09:06:00','dd/mm/yyyy hh24:mi:ss')--and 
ECH.CHANNEL='DSP'
and ch_validfrom=(select min(CH_VALIDFROM) from emcdu.ext_contract_history 
where co_id=ech.co_id and ch_status='a')
group by channel, TO_CHAR(ECH.CH_VALIDFROM,'mm/dd/yyyy hh24:mi') ORDER BY 
TO_CHAR(ECH.CH_VALIDFROM,'mm/dd/yyyy hh24:mi') ASC;

上述查询的输出:

TO_CHAR(ECH.CH_VALIDFROM,'mm/dd/yyyy hh24:mi')         COUNT
11/21/2018 09:00                                       1
11/21/2018 09:01                                       2
11/21/2018 09:02                                       1
11/21/2018 09:03                                       1
11/21/2018 09:05                                       1

2 个答案:

答案 0 :(得分:2)

您可以在下面尝试

select trunc(ECH.CH_VALIDFROM, 'mi') -  1/24/60 * mod(extract (minute from cast(ECH.CH_VALIDFROM as timestamp), 3), count(1) count
from emcdu.ext_contract_history ech
where ch_status='a' and CH_VALIDFROM BETWEEN TO_DATE('21/11/2018 
09:00:00','dd/mm/yyyy hh24:mi:ss') 
AND TO_DATE ('21/11/2018 09:06:00','dd/mm/yyyy hh24:mi:ss')--and 
ECH.CHANNEL='DSP'
and ch_validfrom=(select min(CH_VALIDFROM) from emcdu.ext_contract_history 
where co_id=ech.co_id and ch_status='a')
group by channel, trunc(ECH.CH_VALIDFROM, 'mi') -  1/24/60 * mod(extract (minute from cast(ECH.CH_VALIDFROM as timestamp)), 3) 
ORDER BY 
trunc(ECH.CH_VALIDFROM, 'mi') -  1/24/60 * mod(extract (minute from cast(ECH.CH_VALIDFROM as timestamp)), 3) ASC;

答案 1 :(得分:1)

Open Browser    about:blank    headlesschrome
Open Browser    about:blank    chrome