I have a table of electricity usage over monthly periods in MS Access. I want to create a table or a query that will only display the total sum of electricity usage. Here's a sample of my starting table:
ID, Mete开发者_如何学运维rID, DateRead, kWh
--------------------------
1, 15256, 10/9/2010, 1710
2, 15256, 11/1/2010, 4790
3, 15256, 12/1/2010, 5390
4, 15256, 1/1/2011, 7590
5, 12557, 10/1/2010, 681
6, 12257, 11/1/2010, 710
7, 12257, 11/1/2010, 710
And so on. I want to sum the kWh column where the Meter ID's are the same. So it would look like this
MeterID, kWh
------------
15256, Some sum
12257,Some sum
If I were writing this in C I could do this in 2 seconds. Unfortunately, this is my first real journey into SQL/Databases so I'm not very familiar with them. Any help would be appreciated. Sorry if it's trivial.
SELECT MeterID, SUM(kWh) FROM usage GROUP BY MeterID
SUM() is a aggregate function that goes through every row of your query and sums the argument (here kWh
). You need the GROUP BY
to tell SUM() to add kWh
for the corresponding MeterID.
GROUP BY
精彩评论