sql server - Group By not grouping as expected? -
i'm trying calculate monthly sum, using query.
select fld_year , fld_month , convert(nvarchar(7), fld_consdate, 121) fld_consdate , fld_consumptionvalue , fld_id ( select year(dateadd(minute, fld_tzam - 6360, fld_consdatetime)) fld_year , month(dateadd(minute, fld_tzam- 6360, fld_consdatetime)) fld_month , dateadd(month, datediff(month, 0, fld_consdatetime), 0) fld_consdate , sum(fld_energytotalindexvalue) fld_consumptionvalue , max(fld_id) fld_id tbl_em_deviceenergyhourlyconsumption fld_deleted = 0 , fld_active = 1 , fld_calculationstate = 2 , fld_deviceid in (18) , fld_consdatetime >= dateadd(minute, -1*(fld_tzam) + 6360, '2017-05-01 00:00:00') , fld_consdatetime < dateadd(minute, -1*(fld_tzam) + 6360, '2017-08-01 00:00:00') , fld_deviceorganizationid in (select value dbo.fn_organizationsublist(2)) group year(dateadd(minute, fld_tzam - 6360, fld_consdatetime)), month(dateadd(minute, fld_tzam - 6360, fld_consdatetime)), dateadd(month, datediff(month, 0, fld_consdatetime), 0), )t order fld_consdate asc
the result expect
fld_year fld_month fld_consdate fld_consumptionvalue fld_id 2017 5 2017-05 294.1340 2017 6 2017-06 222.7527 2017 7 2017-07 318.2111
but instead 2 values each month
fld_year fld_month fld_consdate fld_consumptionvalue fld_id 2017 5 2017-05 260.2158 2017 6 2017-06 193.4958 2017 5 2017-06 33.9182 2017 6 2017-07 29.2569 2017 7 2017-07 264.8147 2017 7 2017-08 53.3964
i can't seem figure out tips me? can show me logic gone wrong? in advance!
if want 1 row per month, why including third aggregation key? seem want:
select year(dateadd(minute, fld_tzam - 6360, fld_consdatetime)) fld_year, month(dateadd(minute, fld_tzam - 6360, fld_consdatetime)) fld_month, dateadd(month, datediff(month, 0, min(fld_consdatetime)), 0) fld_consdate, . . . group year(dateadd(minute, fld_tzam - 6360, fld_consdatetime)), month(dateadd(minute, fld_tzam - 6360, fld_consdatetime))
Comments
Post a Comment