根据值筛选列并获取计数
Filter a column based on value and get count
我有一列Startime datetime2(7)
,数据格式是2019-05-15 22:33:55.0000000
。
我正在尝试过滤列并按小时获取计数
SELECT *
FROM vehicles
WHERE vendor = 'ford'
AND starttime BETWEEN '05/01/2019 00:00:00' AND '05/15/2019 23:59:59'
此查询returns日期之间的所有结果;谁能建议如何按小时查询和获取计数?
例如,在 2019 年 5 月 1 日到 2019 年 5 月 15 日之间,计数和结果类似于
- 0 hour countX
- 1 hour countY
- 2 hour countZ
- ...
- ...
- 23 hour countXY
天数之间的所有计数(预期结果为 24 行)
检查这个。这将给出你想要的输出,只有小时的细分 -
SELECT
CAST(DATEPART(HH,starttime) AS VARCHAR) +' Hour Count ' + CAST(COUNT(*) AS VARCHAR)
FROM vehicles
WHERE vendor='ford'
AND starttime BETWEEN '05/01/2019 00:00:00' AND '05/15/2019 23:59:59'
GROUP BY DATEPART(HH,starttime)
我有一列Startime datetime2(7)
,数据格式是2019-05-15 22:33:55.0000000
。
我正在尝试过滤列并按小时获取计数
SELECT *
FROM vehicles
WHERE vendor = 'ford'
AND starttime BETWEEN '05/01/2019 00:00:00' AND '05/15/2019 23:59:59'
此查询returns日期之间的所有结果;谁能建议如何按小时查询和获取计数?
例如,在 2019 年 5 月 1 日到 2019 年 5 月 15 日之间,计数和结果类似于
- 0 hour countX
- 1 hour countY
- 2 hour countZ
- ...
- ...
- 23 hour countXY
天数之间的所有计数(预期结果为 24 行)
检查这个。这将给出你想要的输出,只有小时的细分 -
SELECT
CAST(DATEPART(HH,starttime) AS VARCHAR) +' Hour Count ' + CAST(COUNT(*) AS VARCHAR)
FROM vehicles
WHERE vendor='ford'
AND starttime BETWEEN '05/01/2019 00:00:00' AND '05/15/2019 23:59:59'
GROUP BY DATEPART(HH,starttime)