I have a result set, which consists of a datefield and value column.
The date column can be a range from week, month, several months to years. In the format YYYY-mm-dd hh:mm:ss.ssss (2015-03-01 00:00:00.000 for example)
The value column is a simple decimal.
I am having trouble getting the results to group the way I want.
From the query below you may be able to see what I am trying to achieve. I am selecting from my results set, the relative month, hourslot, day and then value.
SELECT
CONVERT(VARCHAR(2), datefield, 108) AS hourSlot,
SUM(value) AS hourTotal,
DATEPART(day,datefield) as day,
CASE CONVERT(VARCHAR(2), datefield, 108)
WHEN @stationStartTime
THEN 1
ELSE 0
END AS sortOrder
FROM
@hourTotalTemp
WHERE
DATEPART(MM, datefield) = 3
GROUP BY
DATEPART(day,datefield)
ORDER BY
day, sortOrder, CONVERT(VARCHAR(2), datefield, 108)
As you may be able to see.
I need the results to be grouped by the hour. And then sorted by start time of the station, but also the date of the month.
EDIT:
As above, the end result for the query.
Would be for it to return the hour slot, starting from the defined "start time", the relative day(s) for the month (1,2,3,4,5,6,7th etc), and then the value for those days.
Result set for @hourtotalTemp
datefield value
------------------------------
2015-02-01 00:00:00.000 0
2015-02-01 01:00:00.000 0
2015-02-01 02:00:00.000 0
2015-02-01 03:00:00.000 0
2015-02-01 04:00:00.000 0
2015-02-01 05:00:00.000 0
Edit
This is what I would like to achieve with the query. The hours along the top, day on the left. And data in the middle.