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
Post a Comment