I have a table like this:
ModuleID TimeStamp Value TotalPerDay
1 8/5/2012 9:00 PM 0.25 0.25
2 8/5/2012 9:00 PM 0.15 0.15
3 8/5/2012 9:00 PM 0 0
4 8/5/2012 9:00 PM 1.25 1.25
1 8/5/2012 10:00 PM 0.05 0.30
3 8/5/2012 10:00 PM 0.89 0.89
4 8/5/2012 10:00 PM 0.14 1.39
4 8/5/2012 11:00 PM 0.70 2.09
4 8/5/2012 12:00 PM 0.43 2.52
...
And I have 15000 modules updating this table every hour. In addition, at midnight, the TotalPerDay values get reset.
I am struggling with a query that should pull the latest TotalPerDay value for a given set of ModuleID‘s.
I tried using a GROUP BY clause on ModuleID. But how can I retrieve the latest TotalPerDay value without using an aggregating function?
By the way, I am using SQL Server 2008.
Thanks in advance!
If I understand your question correctly, then this should give you the latest total per day by module (if time stamp is unique):
Based on your comments to Gordon, you are looking for a SUM per Day per module? If yes, then here you go: