Reputation: 58444
I have a T-SQL query as below which queries a table holding the search data and gets the search hour and count of the rows for that search hour.
SELECT DATEPART(HOUR, aps.CreatedOn) AS SearchHour, COUNT(*) AS ItemCOUNT
FROM ASearches aps
GROUP BY DATEPART(HOUR, aps.CreatedOn)
ORDER BY SearchHour;
As you can see, this does't produce a great result. However, I know that if I use a Pivot table for this and have the hours as column names, that would be better. I tried but I have been failing so far.
Any idea how?
Upvotes: 2
Views: 3882
Reputation: 6574
Something like this:
SELECT *
FROM (SELECT DATEPART(HOUR, CreatedOn) AS SearchHour
FROM ASearches) aps
PIVOT (COUNT([SearchHour]) FOR SearchHour IN
( [0], [1], [2], [3], [4], [5],
[6], [7], [8], [9], [10], [11],
[12], [13], [14], [15], [16], [17],
[18], [19], [20], [21], [22], [23])) as pvt
Upvotes: 8