我有一个子查询,只返回一个日期时间类型的列:
LOG_DATE --------------------- 2014-06-30 08:15:00 2014-07-01 08:00:00 2014-07-01 08:45:00 2014-07-01 10:00:00 2014-07-01 13:00:00 2014-07-01 14:00:00 2014-07-01 14:30:00 2014-07-01 14:55:00 2014-07-02 08:10:00 2014-07-02 09:00:00 2014-07-02 09:10:00 2014-07-02 09:25:00 2014-07-02 09:40:00 2014-07-02 10:10:00
我试图使用这个应该返回的子查询来编写脚本:
DATE | HOUR | TOTAL ---------------------------------- 2014-06-30 | 08:00:00 | 1 2014-07-01 | 08:00:00 | 2 2014-07-01 | 10:00:00 | 1 2014-07-01 | 13:00:00 | 1 2014-07-01 | 14:00:00 | 3 2014-07-02 | 08:00:00 | 1 2014-07-02 | 09:00:00 | 4 2014-07-02 | 10:00:00 | 1
有人可以帮助我如何获得它吗?
答案 0 :(得分:1)
试试这个:
SELECT CONVERT(DATE,LOG_DATE) [DATE],
DATEPART(HOUR,LOG_DATE) [HOUR],
COUNT(*) TOTAL
FROM YourTable
GROUP BY CONVERT(DATE,LOG_DATE),
DATEPART(HOUR,LOG_DATE)
ORDER BY CONVERT(DATE,LOG_DATE),
DATEPART(HOUR,LOG_DATE)
结果是:
╔════════════╦══════╦═══════╗
║ DATE ║ HOUR ║ TOTAL ║
╠════════════╬══════╬═══════╣
║ 2014-06-30 ║ 8 ║ 1 ║
║ 2014-07-01 ║ 8 ║ 2 ║
║ 2014-07-01 ║ 10 ║ 1 ║
║ 2014-07-01 ║ 13 ║ 1 ║
║ 2014-07-01 ║ 14 ║ 3 ║
║ 2014-07-02 ║ 8 ║ 1 ║
║ 2014-07-02 ║ 9 ║ 4 ║
║ 2014-07-02 ║ 10 ║ 1 ║
╚════════════╩══════╩═══════╝
和here is一个带有演示的方形小说。
答案 1 :(得分:1)
这应该让你开始:
SELECT CONVERT(VARCHAR(10), LOG_DATE, 120), CONVERT(VARCHAR(2), LOG_DATE, 108)+':00:00', COUNT(*)
FROM TABLE1
GROUP BY CONVERT(VARCHAR(10), LOG_DATE, 120), CONVERT(VARCHAR(2), LOG_DATE, 108)+':00:00'
ORDER BY CONVERT(VARCHAR(10), LOG_DATE, 120), CONVERT(VARCHAR(2), LOG_DATE, 108)+':00:00'