[英]SQL Query for group by on Time Interval
我有一個查詢,它根據時間返回記錄,例如
+-------------+--------------+ | RenewalTime | RenewalCount | +-------------+--------------+ | 1 | 2345 | | 2 | 189 | | 3 | 789 | | 4 | 7676 | | 5 | 9876 | | 6 | 9762 | +-------------+--------------+
但是我想像下面這樣顯示它,其中它基於5分鍾組顯示數據
+-------------+--------------+ | RenewalTime | RenewalCount | +-------------+--------------+ | 0-5 | 2345 | | 5-10 | 189 | +-------------+--------------+
Select Round((Cast(i.Modification_Date As Date) - Cast(Refill_Date As Date)) * 24 * 60,0) RenewalTime, count(1) RenewalCount
from refill,subscription_interval i where Trunc(Refill_Date) > '18-Nov-13'
And (Cast(i.Modification_Date As Date) - Cast(Refill_Date As Date)) * 24 * 60 > 0
and (cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60 < 1000
group by Round((Cast(i.Modification_Date As Date) - Cast(Refill_Date As Date)) * 24 * 60,0)
order by RenewalTime;
您可以嘗試以下邏輯:
SELECT
CASE
WHEN YOURCOL1 <= 5
THEN
'1-5'
WHEN YOURCOL1 <= 10
THEN
'6-10'
ELSE
'others'
END
AS YOURCOL1,
COUNT ( YOURCOL2 ) AS N
FROM
YOURTABLE
GROUP BY
CASE
WHEN YOURCOL1 <= 5
THEN
'1-5'
WHEN YOURCOL1 <= 10
THEN
'6-10'
ELSE
'others'
END;
我認為該查詢可以創建您想要的東西:
select to_char((t1.RenewalTime-1)*5)|| '-' ||to_char(t1.RenewalTime*5) as RenewalTime,RenewalCount
from
(Select Round((Cast(i.Modification_Date As Date) - Cast(Refill_Date As Date)) * 24 * 60,0) RenewalTime, count(1) RenewalCount
from refill,subscription_interval i where Trunc(Refill_Date) > '18-Nov-13'
And (Cast(i.Modification_Date As Date) - Cast(Refill_Date As Date)) * 24 * 60 > 0
and (cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60 < 1000
group by Round((Cast(i.Modification_Date As Date) - Cast(Refill_Date As Date)) * 24 * 60,0)
order by RenewalTime)t1
where t1.RenewalTime < 3;
我可以將RenewTime轉換為5分鍾間隔,如下所示:
((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60 /5
接下來,我將使用下限功能將一個5分鍾的小組與另一個小組區分開:
floor((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60/ 5)
接下來,我將其轉換為RenewalTime列所需的文本,如下所示:
to_char( 5 * floor((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60/ 5)) ||' - ' || to_char( 5 * (floor((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60/ 5)+1))
間隔是這樣的:
0 <=更新時間<5 ==>'0-5'
5 <=續約時間<10 ==>'5-10'
10 <=續約時間<15 ==> '10-15'
15 <=續約時間<20 ==> '15-20'
這將導致此sql語句。
select
to_char( 5 * floor((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60/ 5)) || ' - ' || to_char( 5 * (floor((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60/ 5) +1)) RenewalTime
count(1) renewal_count
from refill,
subscription_interval i
where trunc(refill_date) > to_date('18-nov-13', 'dd-mon-yy')
and (cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60 > 0
and (cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60 < 1000
group by
to_char( 5 * floor((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60/ 5)) || ' - ' || to_char( 5 * floor((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60/ 5) +1)
order by 1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.