简体   繁体   中英

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

+-------------+--------------+
| 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:

((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:

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:

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'

5 <= renewal_time < 10 ==> '5 - 10'

10 <= renewal_time < 15 ==> '10 - 15'

15 <= renewal_time < 20 ==> '15 - 20'

This results in this sql statement.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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