One possibility, if you need to do this often enough: add three computed columns for day, month, year to your table. Those columns are computed automatically based on the timestamp
column, and they're just integer values, so they're easy to use in a GROUP BY
.
To do this, use these T-SQL statements:
ALTER TABLE dbo.ROASTER_FEED ADD TSDay AS DAY(timestamp) PERSISTED
ALTER TABLE dbo.ROASTER_FEED ADD TSMonth AS MONTH(timestamp) PERSISTED
ALTER TABLE dbo.ROASTER_FEED ADD TSYear AS YEAR(timestamp) PERSISTED
Now, you can easily select your data based on any day you wish:
SELECT TSDay, TSMonth, TSYear, SUM(FEED) -- use AVG(FEED) for average values
FROM dbo.ROASTER_FEED
WHERE TSYear = 2011 AND TSMonth = 8 -- or whatever you want to grab from the table!
ORDER BY timestamp
GROUP BY TSDay, TSMonth, TSYear
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…