sql server - TSQL Sum aggregating multiple columns with a group by -
sql server - TSQL Sum aggregating multiple columns with a group by -
hi have 3 tables part of much larger query , trying aggregate of values calculation result doubling sum aggregate totaling rows before grouping occurs.
tables:
sr01
select * sr01 reportkey = 109626
ac95
select * ac95 reportkey = 109626
ac96
select * ac96 reportkey = 109626
note 2 rows in case
query
select 'month' [period], isnull(zone.zoneid,'') zoneid, isnull(zone.zonename,'') zonename, isnull(region.regioncode,'') regioncode, isnull(region.regionname,'') regionname, branch.branchid, isnull(branch.branchname,'') branchname, sr01.servicingrep, isnull(lcrep.repname,'') repname, ac95.preptime, ac95.svcpreptime , ac95.traveltime , ac95.svctraveltime , ac95.visittime , ac95.svcvisittime, sum(ac95.preptime + ac95.svcpreptime + ac95.traveltime + ac95.svctraveltime + ac95.visittime + ac95.svcvisittime) hoursmonth, sr01.reportkey dbo.sr01 inner bring together ac95 on ac95.reportkey = sr01.reportkey inner bring together ac96 on ac96.reportkey = ac95.reportkey left bring together dbo.requestsnonreportview on requestsnonreportview.reportkey = sr01.reportkey left bring together dbo.lcrep on sr01.servicingrep = lcrep.repid left bring together dbo.branch on sr01.servicingbranch = branch.branchid left bring together dbo.region on region.regioncode = branch.region left bring together dbo.zone on zone.zoneabbrev = region.zone isnull(sr01.servicingbranch,'-') <> '-' , sr01.[status]='x' , sr01.requesttype <> 'mn' , sr01.datecomplete between dateadd(month, datediff(month, 0, @dateto), 0) , @dateto , (ac95.onoffsite = 'on' or ac95.onoffsitesvc = 'on') , sr01.servicingrep = @servicingrep grouping zone.zoneid, zone.zonename, region.regioncode, region.regionname, branch.branchid, branchname, sr01.servicingrep, lcrep.repname, sr01.reportkey,ac95.preptime,ac95.svcpreptime , ac95.traveltime , ac95.svctraveltime , ac95.visittime , ac95.svcvisittime
result
note values preptim, traveltime , visittime not aggregated , appear correctly. when using sum aggregate add together them total double expected @ 11 rather 5.5.
thinking due multiple rows in ac96 removed grouping , there 2 rows shown. must why sum aggreagte doubling value.
question
how correctly term query sum values correctly without doubling, or more, number when there multiple rows in ac96?
please seek this,
;with cte_preresult ( select distinct 'month' [period], isnull(zone.zoneid,'') zoneid, isnull(zone.zonename,'') zonename, isnull(region.regioncode,'') regioncode, isnull(region.regionname,'') regionname, branch.branchid, isnull(branch.branchname,'') branchname, sr01.servicingrep, isnull(lcrep.repname,'') repname, ac95.preptime, ac95.svcpreptime, ac95.traveltime, ac95.svctraveltime, ac95.visittime, ac95.svcvisittime, sr01.reportkey dbo.sr01 inner bring together ac95 on ac95.reportkey = sr01.reportkey inner bring together ac96 on ac96.reportkey = ac95.reportkey left bring together dbo.requestsnonreportview on requestsnonreportview.reportkey = sr01.reportkey left bring together dbo.lcrep on sr01.servicingrep = lcrep.repid left bring together dbo.branch on sr01.servicingbranch = branch.branchid left bring together dbo.region on region.regioncode = branch.region left bring together dbo.zone on zone.zoneabbrev = region.zone isnull(sr01.servicingbranch,'-') <> '-' , sr01.[status]='x' , sr01.requesttype <> 'mn' , sr01.datecomplete between dateadd(month, datediff(month, 0, @dateto), 0) , @dateto , (ac95.onoffsite = 'on' or ac95.onoffsitesvc = 'on') , sr01.servicingrep = @servicingrep ) select [period], zoneid, zonename, regioncode, regionname, branchid, branchname, servicingrep, repname, sum(ac95.preptime + ac95.svcpreptime + ac95.traveltime + ac95.svctraveltime + ac95.visittime + ac95.svcvisittime) hoursmonth, reportkey cte_preresult grouping [period], zoneid, zonename, regioncode, regionname, branchid, branchname, servicingrep, repname, reportkey
sql-server sql-server-2008 tsql aggregate
Comments
Post a Comment