Simple way to use the SUM function in SQL -


so having issues creating sproc report new application reads calculations devices. these readings basic numbers , taken every week. trying combine calculations each month.

i started toying around sum function since havent done sub-queries within a sum function before , have below:

select     (select sum(reading) readings (month(readings.readingdate) = 1) , meterid = 1) january1,     (select sum(reading) readings (month(readings.readingdate) = 1) , meterid = 2) january2,     (select sum(reading) readings (month(readings.readingdate) = 1) , meterid = 3) january3          readings year(readings.readingdate) = 2013 

my tables , fields getting info pulled are:

meters: meterid, metername

readings: id, userid, meterid, reading, readingdate

i'm aware not in stored procedure format right now, have feeling getting in on head current format because there 19 meters , if take 12 months of year going have 1 monster length of sproc feel simpler. awesome.

thanks!

use "group by":

select     month(readings.readingdate), meterid, sum(reading) readings      year(readings.readingdate) = 2013 group      month(readings.readingdate), meterid; 

this should give 19 * 12 rows, 1 each combination of reading month , meter.

(didn't test this, syntax should close.)


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -