简体   繁体   English

SQL查询按时间间隔进行分组

[英]SQL Query for group by on Time Interval

I have a query which returns records according to time like 我有一个查询,它根据时间返回记录,例如

+-------------+--------------+
| RenewalTime | RenewalCount |
+-------------+--------------+
|           1 |         2345 |
|           2 |          189 |
|           3 |          789 |
|           4 |         7676 |
|           5 |         9876 |
|           6 |         9762 |
+-------------+--------------+

but i want to show it like following in which it shows data on the base of 5 min groups 但是我想像下面这样显示它,其中它基于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;

You can try this logic: 您可以尝试以下逻辑:

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;

i think that this query can create the thing which you want: 我认为该查询可以创建您想要的东西:

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;

I would convert this RenewTime to 5 minute intervals like this: 我可以将RenewTime转换为5分钟间隔,如下所示:

((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60 /5

Next, I would use the floor function to distinguish one 5 minute group from another: 接下来,我将使用下限功能将一个5分钟的小组与另一个小组区分开:

floor((cast(i.modification_date as date) - cast(refill_date as date)) * 24 * 60/ 5)

Next, I would convert this to the text you want for the RenewalTime column as follows: 接下来,我将其转换为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))

The intervals are like this: 间隔是这样的:

0 <= renewal_time < 5 ==> '0 - 5' 0 <=更新时间<5 ==>'0-5'

5 <= renewal_time < 10 ==> '5 - 10' 5 <=续约时间<10 ==>'5-10'

10 <= renewal_time < 15 ==> '10 - 15' 10 <=续约时间<15 ==> '10-15'

15 <= renewal_time < 20 ==> '15 - 20' 15 <=续约时间<20 ==> '15-20'

This results in this sql statement. 这将导致此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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM