答案 0 :(得分:2)
你可以这样做:
| Item | 8AM-10AM | 10AM-12AM | 12PM-2PM | 2AM-4PM | >4PM |
|--------|----------|-----------|----------|---------|------|
| Item 1 | 2 | 4 | 1 | 0 | 0 |
| Item 2 | 0 | 0 | 1 | 2 | 2 |
示例结果:
PIVOT
<强>更新强>
您可以将SELECT *
FROM
(
SELECT
Item,
Alias
FROM Table1
INNER JOIN Ranges ON CAST(DateTimeCol AS TIME) >= [Start]
AND CAST(DateTimeCol AS TIME) < [End]
) AS t
PIVOT
(
COUNT(Alias)
FOR Alias IN([8AM-10AM], [10AM-12AM], [12PM-2PM],[2PM-4PM], [>4PM])
) AS p;
表运算符与临时表一起使用,其中包含以下所有时间范围:
{{1}}