I/p
21-MAR-14 5:10 PM 21-MAR-14 5:20 PM web
21-MAR-14 5:18 PM 21-MAR-14 5:20 PM mob
21-MAR-14 5:19 PM 21-MAR-14 5:24 PM mob
o/p should be web mob
21-MAR-14 11:30 AM 1 1
21-MAR-14 11:35 AM 3 1
21-MAR-14 11:40 AM 1 1
根据时间差异对列进行分组
答案 0 :(得分:1)
我会从生成你想要的时间开始。然后,您将此列表添加到查询的其余部分。但是,您的问题没有查询。
这是一种生成时间的方法:
with nums as (
select 5*(level - 1) as n from dual
connect by level <= 24*60/5
)
select to_timestamp(to_char(mod(n/60, 24), '00')||':'||to_char(mod(n, 60), '00'), 'HH24:MI')
from nums
答案 1 :(得分:1)
我希望要求不会有其他变化:-P 再次更改查询。 假设您的数据库是Oracle 11g,请使用以下查询:
WITH date_table AS (SELECT to_date('21-MAR-2014', 'DD-MON-YYYY') ddate FROM dual), --> Your date here
date_group AS (SELECT trunc(ddate) + (5/(24 * 60)) * LEVEL ddate
FROM date_table
CONNECT BY trunc(ddate) + (5/(24 * 60)) * LEVEL <= trunc(ddate) + 1),
user_table1 AS (SELECT CASE WHEN ut.start_date < td.ddate
THEN td.ddate
ELSE (SELECT MIN(a.ddate) FROM date_group a WHERE a.ddate >= ut.start_date)
END start_date1,
(SELECT nvl(MIN(b.ddate), td.ddate + 1) FROM date_group b WHERE b.ddate >= ut.end_date) end_date1,
start_date,
end_date,
ut.apptype
FROM user_table ut --> Your table here
JOIN date_table td ON trunc(ut.start_date) = td.ddate
OR trunc(ut.end_date) = td.ddate),
final_table AS (SELECT ddate,
apptype
FROM user_table1 ut1
RIGHT OUTER JOIN date_group dg
ON (dg.ddate BETWEEN ut1.start_date1 AND (CASE WHEN ut1.end_date - ut1.start_date > 5/(24*60)
THEN ut1.end_date1
ELSE ut1.start_date1
END)))
SELECT *
FROM final_table
pivot (SUM(1) FOR apptype IN ('web' AS web, 'mob' AS mob))
ORDER BY 1;
SQL小提琴(http://sqlfiddle.com/#!4/a7606/2)
输出:
| DDATE | WEB | MOB |
|------------------------------|--------|--------|
| March, 21 2014 00:05:00+0000 | (null) | (null) |
| March, 21 2014 00:10:00+0000 | (null) | (null) |
| March, 21 2014 00:15:00+0000 | (null) | (null) |
| March, 21 2014 00:20:00+0000 | (null) | (null) |
| March, 21 2014 00:25:00+0000 | (null) | (null) |
| March, 21 2014 00:30:00+0000 | (null) | (null) |
| March, 21 2014 00:35:00+0000 | (null) | (null) |
| March, 21 2014 00:40:00+0000 | (null) | (null) |
| March, 21 2014 00:45:00+0000 | (null) | (null) |
| March, 21 2014 00:50:00+0000 | (null) | (null) |
| March, 21 2014 00:55:00+0000 | (null) | (null) |
| March, 21 2014 01:00:00+0000 | (null) | (null) |
| March, 21 2014 01:05:00+0000 | (null) | (null) |
| March, 21 2014 01:10:00+0000 | (null) | (null) |
| March, 21 2014 01:15:00+0000 | (null) | (null) |
| March, 21 2014 01:20:00+0000 | (null) | (null) |
| March, 21 2014 01:25:00+0000 | (null) | (null) |
| March, 21 2014 01:30:00+0000 | (null) | (null) |
| March, 21 2014 01:35:00+0000 | (null) | (null) |
| March, 21 2014 01:40:00+0000 | (null) | (null) |
| March, 21 2014 01:45:00+0000 | (null) | (null) |
| March, 21 2014 01:50:00+0000 | (null) | (null) |
| March, 21 2014 01:55:00+0000 | (null) | (null) |
| March, 21 2014 02:00:00+0000 | (null) | (null) |
| March, 21 2014 02:05:00+0000 | (null) | (null) |
| March, 21 2014 02:10:00+0000 | (null) | (null) |
| March, 21 2014 02:15:00+0000 | (null) | (null) |
| March, 21 2014 02:20:00+0000 | (null) | (null) |
| March, 21 2014 02:25:00+0000 | (null) | (null) |
| March, 21 2014 02:30:00+0000 | (null) | (null) |
| March, 21 2014 02:35:00+0000 | (null) | (null) |
| March, 21 2014 02:40:00+0000 | (null) | (null) |
| March, 21 2014 02:45:00+0000 | (null) | (null) |
| March, 21 2014 02:50:00+0000 | (null) | (null) |
| March, 21 2014 02:55:00+0000 | (null) | (null) |
| March, 21 2014 03:00:00+0000 | (null) | (null) |
| March, 21 2014 03:05:00+0000 | (null) | (null) |
| March, 21 2014 03:10:00+0000 | (null) | (null) |
| March, 21 2014 03:15:00+0000 | (null) | (null) |
| March, 21 2014 03:20:00+0000 | (null) | (null) |
| March, 21 2014 03:25:00+0000 | (null) | (null) |
| March, 21 2014 03:30:00+0000 | (null) | (null) |
| March, 21 2014 03:35:00+0000 | (null) | (null) |
| March, 21 2014 03:40:00+0000 | (null) | (null) |
| March, 21 2014 03:45:00+0000 | (null) | (null) |
| March, 21 2014 03:50:00+0000 | (null) | (null) |
| March, 21 2014 03:55:00+0000 | (null) | (null) |
| March, 21 2014 04:00:00+0000 | (null) | (null) |
| March, 21 2014 04:05:00+0000 | (null) | (null) |
| March, 21 2014 04:10:00+0000 | (null) | (null) |
| March, 21 2014 04:15:00+0000 | (null) | (null) |
| March, 21 2014 04:20:00+0000 | (null) | (null) |
| March, 21 2014 04:25:00+0000 | (null) | (null) |
| March, 21 2014 04:30:00+0000 | (null) | (null) |
| March, 21 2014 04:35:00+0000 | (null) | (null) |
| March, 21 2014 04:40:00+0000 | (null) | (null) |
| March, 21 2014 04:45:00+0000 | (null) | (null) |
| March, 21 2014 04:50:00+0000 | (null) | (null) |
| March, 21 2014 04:55:00+0000 | (null) | (null) |
| March, 21 2014 05:00:00+0000 | (null) | (null) |
| March, 21 2014 05:05:00+0000 | (null) | (null) |
| March, 21 2014 05:10:00+0000 | (null) | (null) |
| March, 21 2014 05:15:00+0000 | (null) | (null) |
| March, 21 2014 05:20:00+0000 | (null) | (null) |
| March, 21 2014 05:25:00+0000 | (null) | (null) |
| March, 21 2014 05:30:00+0000 | (null) | (null) |
| March, 21 2014 05:35:00+0000 | (null) | (null) |
| March, 21 2014 05:40:00+0000 | (null) | (null) |
| March, 21 2014 05:45:00+0000 | (null) | (null) |
| March, 21 2014 05:50:00+0000 | (null) | (null) |
| March, 21 2014 05:55:00+0000 | (null) | (null) |
| March, 21 2014 06:00:00+0000 | (null) | (null) |
| March, 21 2014 06:05:00+0000 | (null) | (null) |
| March, 21 2014 06:10:00+0000 | (null) | (null) |
| March, 21 2014 06:15:00+0000 | (null) | (null) |
| March, 21 2014 06:20:00+0000 | (null) | (null) |
| March, 21 2014 06:25:00+0000 | (null) | (null) |
| March, 21 2014 06:30:00+0000 | (null) | (null) |
| March, 21 2014 06:35:00+0000 | (null) | (null) |
| March, 21 2014 06:40:00+0000 | (null) | (null) |
| March, 21 2014 06:45:00+0000 | (null) | (null) |
| March, 21 2014 06:50:00+0000 | (null) | (null) |
| March, 21 2014 06:55:00+0000 | (null) | (null) |
| March, 21 2014 07:00:00+0000 | (null) | (null) |
| March, 21 2014 07:05:00+0000 | (null) | (null) |
| March, 21 2014 07:10:00+0000 | (null) | (null) |
| March, 21 2014 07:15:00+0000 | (null) | (null) |
| March, 21 2014 07:20:00+0000 | (null) | (null) |
| March, 21 2014 07:25:00+0000 | (null) | (null) |
| March, 21 2014 07:30:00+0000 | (null) | (null) |
| March, 21 2014 07:35:00+0000 | (null) | (null) |
| March, 21 2014 07:40:00+0000 | (null) | (null) |
| March, 21 2014 07:45:00+0000 | (null) | (null) |
| March, 21 2014 07:50:00+0000 | (null) | (null) |
| March, 21 2014 07:55:00+0000 | (null) | (null) |
| March, 21 2014 08:00:00+0000 | (null) | (null) |
| March, 21 2014 08:05:00+0000 | (null) | (null) |
| March, 21 2014 08:10:00+0000 | (null) | (null) |
| March, 21 2014 08:15:00+0000 | (null) | (null) |
| March, 21 2014 08:20:00+0000 | (null) | (null) |
| March, 21 2014 08:25:00+0000 | (null) | (null) |
| March, 21 2014 08:30:00+0000 | (null) | (null) |
| March, 21 2014 08:35:00+0000 | (null) | (null) |
| March, 21 2014 08:40:00+0000 | (null) | (null) |
| March, 21 2014 08:45:00+0000 | (null) | (null) |
| March, 21 2014 08:50:00+0000 | (null) | (null) |
| March, 21 2014 08:55:00+0000 | (null) | (null) |
| March, 21 2014 09:00:00+0000 | (null) | (null) |
| March, 21 2014 09:05:00+0000 | (null) | (null) |
| March, 21 2014 09:10:00+0000 | (null) | (null) |
| March, 21 2014 09:15:00+0000 | (null) | (null) |
| March, 21 2014 09:20:00+0000 | (null) | (null) |
| March, 21 2014 09:25:00+0000 | (null) | (null) |
| March, 21 2014 09:30:00+0000 | (null) | (null) |
| March, 21 2014 09:35:00+0000 | (null) | (null) |
| March, 21 2014 09:40:00+0000 | (null) | (null) |
| March, 21 2014 09:45:00+0000 | (null) | (null) |
| March, 21 2014 09:50:00+0000 | (null) | (null) |
| March, 21 2014 09:55:00+0000 | (null) | (null) |
| March, 21 2014 10:00:00+0000 | (null) | (null) |
| March, 21 2014 10:05:00+0000 | (null) | (null) |
| March, 21 2014 10:10:00+0000 | (null) | (null) |
| March, 21 2014 10:15:00+0000 | (null) | (null) |
| March, 21 2014 10:20:00+0000 | (null) | (null) |
| March, 21 2014 10:25:00+0000 | (null) | (null) |
| March, 21 2014 10:30:00+0000 | (null) | (null) |
| March, 21 2014 10:35:00+0000 | (null) | (null) |
| March, 21 2014 10:40:00+0000 | (null) | (null) |
| March, 21 2014 10:45:00+0000 | (null) | (null) |
| March, 21 2014 10:50:00+0000 | (null) | (null) |
| March, 21 2014 10:55:00+0000 | (null) | (null) |
| March, 21 2014 11:00:00+0000 | (null) | (null) |
| March, 21 2014 11:05:00+0000 | (null) | (null) |
| March, 21 2014 11:10:00+0000 | (null) | (null) |
| March, 21 2014 11:15:00+0000 | (null) | (null) |
| March, 21 2014 11:20:00+0000 | (null) | (null) |
| March, 21 2014 11:25:00+0000 | (null) | (null) |
| March, 21 2014 11:30:00+0000 | 1 | 1 |
| March, 21 2014 11:35:00+0000 | 3 | 1 |
| March, 21 2014 11:40:00+0000 | 1 | 1 |
| March, 21 2014 11:45:00+0000 | 1 | 1 |
| March, 21 2014 11:50:00+0000 | (null) | 1 |
| March, 21 2014 11:55:00+0000 | (null) | 1 |
| March, 21 2014 12:00:00+0000 | (null) | (null) |
| March, 21 2014 12:05:00+0000 | (null) | (null) |
| March, 21 2014 12:10:00+0000 | (null) | (null) |
| March, 21 2014 12:15:00+0000 | (null) | (null) |
| March, 21 2014 12:20:00+0000 | (null) | (null) |
| March, 21 2014 12:25:00+0000 | (null) | (null) |
| March, 21 2014 12:30:00+0000 | (null) | (null) |
| March, 21 2014 12:35:00+0000 | (null) | (null) |
| March, 21 2014 12:40:00+0000 | (null) | (null) |
| March, 21 2014 12:45:00+0000 | (null) | (null) |
| March, 21 2014 12:50:00+0000 | (null) | (null) |
| March, 21 2014 12:55:00+0000 | (null) | (null) |
| March, 21 2014 13:00:00+0000 | (null) | (null) |
| March, 21 2014 13:05:00+0000 | (null) | (null) |
| March, 21 2014 13:10:00+0000 | (null) | (null) |
| March, 21 2014 13:15:00+0000 | (null) | (null) |
| March, 21 2014 13:20:00+0000 | (null) | (null) |
| March, 21 2014 13:25:00+0000 | (null) | (null) |
| March, 21 2014 13:30:00+0000 | (null) | (null) |
| March, 21 2014 13:35:00+0000 | (null) | (null) |
| March, 21 2014 13:40:00+0000 | (null) | (null) |
| March, 21 2014 13:45:00+0000 | (null) | (null) |
| March, 21 2014 13:50:00+0000 | (null) | (null) |
| March, 21 2014 13:55:00+0000 | (null) | (null) |
| March, 21 2014 14:00:00+0000 | (null) | (null) |
| March, 21 2014 14:05:00+0000 | (null) | (null) |
| March, 21 2014 14:10:00+0000 | (null) | (null) |
| March, 21 2014 14:15:00+0000 | (null) | (null) |
| March, 21 2014 14:20:00+0000 | (null) | (null) |
| March, 21 2014 14:25:00+0000 | (null) | (null) |
| March, 21 2014 14:30:00+0000 | (null) | (null) |
| March, 21 2014 14:35:00+0000 | (null) | (null) |
| March, 21 2014 14:40:00+0000 | (null) | (null) |
| March, 21 2014 14:45:00+0000 | (null) | (null) |
| March, 21 2014 14:50:00+0000 | (null) | (null) |
| March, 21 2014 14:55:00+0000 | (null) | (null) |
| March, 21 2014 15:00:00+0000 | (null) | (null) |
| March, 21 2014 15:05:00+0000 | (null) | (null) |
| March, 21 2014 15:10:00+0000 | (null) | (null) |
| March, 21 2014 15:15:00+0000 | (null) | (null) |
| March, 21 2014 15:20:00+0000 | (null) | (null) |
| March, 21 2014 15:25:00+0000 | (null) | (null) |
| March, 21 2014 15:30:00+0000 | (null) | (null) |
| March, 21 2014 15:35:00+0000 | (null) | (null) |
| March, 21 2014 15:40:00+0000 | (null) | (null) |
| March, 21 2014 15:45:00+0000 | (null) | (null) |
| March, 21 2014 15:50:00+0000 | (null) | (null) |
| March, 21 2014 15:55:00+0000 | (null) | (null) |
| March, 21 2014 16:00:00+0000 | (null) | (null) |
| March, 21 2014 16:05:00+0000 | (null) | (null) |
| March, 21 2014 16:10:00+0000 | (null) | (null) |
| March, 21 2014 16:15:00+0000 | (null) | (null) |
| March, 21 2014 16:20:00+0000 | (null) | (null) |
| March, 21 2014 16:25:00+0000 | (null) | (null) |
| March, 21 2014 16:30:00+0000 | (null) | (null) |
| March, 21 2014 16:35:00+0000 | (null) | (null) |
| March, 21 2014 16:40:00+0000 | (null) | (null) |
| March, 21 2014 16:45:00+0000 | (null) | (null) |
| March, 21 2014 16:50:00+0000 | (null) | (null) |
| March, 21 2014 16:55:00+0000 | (null) | (null) |
| March, 21 2014 17:00:00+0000 | (null) | (null) |
| March, 21 2014 17:05:00+0000 | (null) | (null) |
| March, 21 2014 17:10:00+0000 | 1 | (null) |
| March, 21 2014 17:15:00+0000 | 1 | (null) |
| March, 21 2014 17:20:00+0000 | 1 | 2 |
| March, 21 2014 17:25:00+0000 | (null) | (null) |
| March, 21 2014 17:30:00+0000 | (null) | (null) |
| March, 21 2014 17:35:00+0000 | (null) | (null) |
| March, 21 2014 17:40:00+0000 | (null) | (null) |
| March, 21 2014 17:45:00+0000 | (null) | (null) |
| March, 21 2014 17:50:00+0000 | (null) | (null) |
| March, 21 2014 17:55:00+0000 | (null) | (null) |
| March, 21 2014 18:00:00+0000 | (null) | (null) |
| March, 21 2014 18:05:00+0000 | (null) | (null) |
| March, 21 2014 18:10:00+0000 | (null) | (null) |
| March, 21 2014 18:15:00+0000 | (null) | (null) |
| March, 21 2014 18:20:00+0000 | (null) | (null) |
| March, 21 2014 18:25:00+0000 | (null) | (null) |
| March, 21 2014 18:30:00+0000 | (null) | (null) |
| March, 21 2014 18:35:00+0000 | (null) | (null) |
| March, 21 2014 18:40:00+0000 | (null) | (null) |
| March, 21 2014 18:45:00+0000 | (null) | (null) |
| March, 21 2014 18:50:00+0000 | (null) | (null) |
| March, 21 2014 18:55:00+0000 | (null) | (null) |
| March, 21 2014 19:00:00+0000 | (null) | (null) |
| March, 21 2014 19:05:00+0000 | (null) | (null) |
| March, 21 2014 19:10:00+0000 | (null) | (null) |
| March, 21 2014 19:15:00+0000 | (null) | (null) |
| March, 21 2014 19:20:00+0000 | (null) | (null) |
| March, 21 2014 19:25:00+0000 | (null) | (null) |
| March, 21 2014 19:30:00+0000 | (null) | (null) |
| March, 21 2014 19:35:00+0000 | (null) | (null) |
| March, 21 2014 19:40:00+0000 | (null) | (null) |
| March, 21 2014 19:45:00+0000 | (null) | (null) |
| March, 21 2014 19:50:00+0000 | (null) | (null) |
| March, 21 2014 19:55:00+0000 | (null) | (null) |
| March, 21 2014 20:00:00+0000 | (null) | (null) |
| March, 21 2014 20:05:00+0000 | (null) | (null) |
| March, 21 2014 20:10:00+0000 | (null) | (null) |
| March, 21 2014 20:15:00+0000 | (null) | (null) |
| March, 21 2014 20:20:00+0000 | (null) | (null) |
| March, 21 2014 20:25:00+0000 | (null) | (null) |
| March, 21 2014 20:30:00+0000 | (null) | (null) |
| March, 21 2014 20:35:00+0000 | (null) | (null) |
| March, 21 2014 20:40:00+0000 | (null) | (null) |
| March, 21 2014 20:45:00+0000 | (null) | (null) |
| March, 21 2014 20:50:00+0000 | (null) | (null) |
| March, 21 2014 20:55:00+0000 | (null) | (null) |
| March, 21 2014 21:00:00+0000 | (null) | (null) |
| March, 21 2014 21:05:00+0000 | (null) | (null) |
| March, 21 2014 21:10:00+0000 | (null) | (null) |
| March, 21 2014 21:15:00+0000 | (null) | (null) |
| March, 21 2014 21:20:00+0000 | (null) | (null) |
| March, 21 2014 21:25:00+0000 | (null) | (null) |
| March, 21 2014 21:30:00+0000 | (null) | (null) |
| March, 21 2014 21:35:00+0000 | (null) | (null) |
| March, 21 2014 21:40:00+0000 | (null) | (null) |
| March, 21 2014 21:45:00+0000 | (null) | (null) |
| March, 21 2014 21:50:00+0000 | (null) | (null) |
| March, 21 2014 21:55:00+0000 | (null) | (null) |
| March, 21 2014 22:00:00+0000 | (null) | (null) |
| March, 21 2014 22:05:00+0000 | (null) | (null) |
| March, 21 2014 22:10:00+0000 | (null) | (null) |
| March, 21 2014 22:15:00+0000 | (null) | (null) |
| March, 21 2014 22:20:00+0000 | (null) | (null) |
| March, 21 2014 22:25:00+0000 | (null) | (null) |
| March, 21 2014 22:30:00+0000 | (null) | (null) |
| March, 21 2014 22:35:00+0000 | (null) | (null) |
| March, 21 2014 22:40:00+0000 | (null) | (null) |
| March, 21 2014 22:45:00+0000 | (null) | (null) |
| March, 21 2014 22:50:00+0000 | (null) | (null) |
| March, 21 2014 22:55:00+0000 | (null) | (null) |
| March, 21 2014 23:00:00+0000 | (null) | (null) |
| March, 21 2014 23:05:00+0000 | (null) | (null) |
| March, 21 2014 23:10:00+0000 | (null) | (null) |
| March, 21 2014 23:15:00+0000 | (null) | (null) |
| March, 21 2014 23:20:00+0000 | (null) | (null) |
| March, 21 2014 23:25:00+0000 | (null) | (null) |
| March, 21 2014 23:30:00+0000 | (null) | (null) |
| March, 21 2014 23:35:00+0000 | (null) | (null) |
| March, 21 2014 23:40:00+0000 | (null) | (null) |
| March, 21 2014 23:45:00+0000 | (null) | (null) |
| March, 21 2014 23:50:00+0000 | (null) | (null) |
| March, 21 2014 23:55:00+0000 | (null) | (null) |
| March, 22 2014 00:00:00+0000 | (null) | (null) |
由于我必须多次使用输入日期,所以我采用了一个虚拟表date_table
WITH date_table(ddate) AS
(SELECT to_date('21-MAR-2014', 'DD-MON-YYYY') FROM dual), -- Your Date here
下面的这一部分(date_group)将实现一个看起来像
的表- 3/21/2014 12:05:00 AM - 3/21/2014 12:10:00 AM - ..... - ..... - 3/22/2014 12:00:00 AM
date_group AS (SELECT trunc(ddate) + (5/(24 * 60)) * LEVEL ddate
FROM date_table
CONNECT BY trunc(ddate) + (5/(24 * 60)) * LEVEL <= trunc(ddate) + 1),
此部分将start_date和end_date设置为接下来的5分钟多值
user_table1 AS (SELECT CASE WHEN ut.start_date < td.ddate
THEN td.ddate
ELSE (SELECT MIN(a.ddate) FROM date_group a WHERE a.ddate >= ut.start_date)
END start_date1,
(SELECT nvl(MIN(b.ddate), td.ddate + 1) FROM date_group b WHERE b.ddate >= ut.end_date) end_date1,
start_date,
end_date,
ut.apptype
FROM user_table ut --> Your table here
JOIN date_table td ON trunc(ut.start_date) = td.ddate
OR trunc(ut.end_date) = td.ddate),
下面的查询部分将创建日期(5分钟组)和apptype
final_table AS (SELECT ddate,
apptype
FROM user_table1 ut1
RIGHT OUTER JOIN date_group dg
ON (dg.ddate BETWEEN ut1.start_date1 AND (CASE WHEN ut1.end_date - ut1.start_date > 5/(24*60)
THEN ut1.end_date1
ELSE ut1.start_date1
END)))
最后一部分将创建预期结果
SELECT *
FROM final_table
pivot (SUM(1) FOR apptype IN ('web' AS web, 'mob' AS mob))
ORDER BY 1;
要了解有关枢轴的更多信息,请参阅this link