我有一个查询,它根据时间返回记录,如
+-------------+--------------+ | 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;
答案 0 :(得分:0)
你可以试试这个逻辑:
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;
答案 1 :(得分:0)
我认为这个查询可以创建你想要的东西:
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;
答案 2 :(得分:0)
我会将此RenewTime转换为5分钟间隔,如下所示:
((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60 /5
接下来,我会使用floor函数来区分一个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&lt; = renewal_time&lt; 5 ==&gt; '0 - 5'
5&lt; = renewal_time&lt; 10 ==&gt; '5 - 10'
10&lt; = renewal_time&lt; 15 ==&gt; '10 - 15'
15&lt; = renewal_time&lt; 20 ==&gt; '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;