2014-11-06 20 views
0

嗨有三个表作为一个更大的查询的一部分,我试图聚合一些计算值,但结果是加倍作为总和聚合总计所有行之前该组发生。TSQL总和多个列与一个组由

表:

SR01

select * from sr01 where ReportKey = 109626 

enter image description here

AC95

select * from ac95 where ReportKey = 109626 

enter image description here

AC96

select * from ac96 where ReportKey = 109626 

enter image description here

注意,在这种情况下

查询

SELECT 'Month' AS [Period], 
    ISNULL(Zone.ZoneID,'') AS ZoneID, 
    ISNULL(Zone.ZoneName,'') AS ZoneName, 
    ISNULL(Region.RegionCode,'') AS RegionCode, 
    ISNULL(Region.RegionName,'') AS RegionName, 
    Branch.BranchID, 
    ISNULL(Branch.BranchName,'') AS BranchName, 
    SR01.ServicingRep, 
    ISNULL(LCRep.RepName,'') AS 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) AS HoursMonth, 
    SR01.ReportKEy 
FROM dbo.SR01 
    INNER JOIN AC95 ON AC95.ReportKey = SR01.ReportKey 
    INNER JOIN AC96 ON AC96.ReportKey = AC95.ReportKey 
    LEFT JOIN dbo.RequestsNonReportView ON RequestsNonReportView.ReportKey = SR01.ReportKey 
    LEFT JOIN dbo.LCRep ON SR01.ServicingRep = LCRep.RepID 
    LEFT JOIN dbo.Branch ON SR01.ServicingBranch = Branch.BranchID 
    LEFT JOIN dbo.Region ON Region.RegionCode = Branch.Region 
    LEFT JOIN dbo.Zone ON Zone.ZoneAbbrev = Region.Zone 
WHERE ISNULL(SR01.ServicingBranch,'-') <> '-' 
    AND SR01.[Status]='X' 
    AND SR01.RequestType <> 'MN' 
    AND SR01.DateComplete BETWEEN DATEADD(month, DATEDIFF(month, 0, @DateTo), 0) AND @DateTo 
    AND (AC95.OnOffSite = 'ON' OR AC95.OnOffSiteSvc = 'ON') 
    AND SR01.ServicingRep = @ServicingRep 
GROUP BY 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 
的2行0

结果

请注意,值PrepTim,TravelTime和VisitTime未汇总并显示正确。当使用SUM聚合添加它们时,总数是预期的11倍而不是5.5倍。

enter image description here

认为这可能是由于在AC96多行我删除了GROUP BY和有显示两行。这一定是为什么SUM聚合将价值翻倍的原因。

enter image description here

问题

如何正确正确项的查询到的值相加没有它加倍,或更多,当有AC96多行数?

回答

1

请试试这个,

;WITH cte_PreResult AS 
(
SELECT DISTINCT 'Month' AS [Period], 
    ISNULL(Zone.ZoneID,'') AS ZoneID, 
    ISNULL(Zone.ZoneName,'') AS ZoneName, 
    ISNULL(Region.RegionCode,'') AS RegionCode, 
    ISNULL(Region.RegionName,'') AS RegionName, 
    Branch.BranchID, 
    ISNULL(Branch.BranchName,'') AS BranchName, 
    SR01.ServicingRep, 
    ISNULL(LCRep.RepName,'') AS RepName, 
    AC95.PrepTime, 
    AC95.SvcPrepTime, 
    AC95.TravelTime, 
    AC95.SvcTravelTime, 
    AC95.VisitTime, 
    AC95.SvcVisitTime, 
    SR01.ReportKEy 
FROM dbo.SR01 
INNER JOIN AC95 
    ON AC95.ReportKey = SR01.ReportKey 
INNER JOIN AC96 
    ON AC96.ReportKey = AC95.ReportKey 
LEFT JOIN dbo.RequestsNonReportView 
    ON RequestsNonReportView.ReportKey = SR01.ReportKey 
LEFT JOIN dbo.LCRep 
    ON SR01.ServicingRep = LCRep.RepID 
LEFT JOIN dbo.Branch 
    ON SR01.ServicingBranch = Branch.BranchID 
LEFT JOIN dbo.Region 
    ON Region.RegionCode = Branch.Region 
LEFT JOIN dbo.Zone 
    ON Zone.ZoneAbbrev = Region.Zone 
WHERE ISNULL(SR01.ServicingBranch,'-') <> '-' 
    AND SR01.[Status]='X' 
    AND SR01.RequestType <> 'MN' 
    AND SR01.DateComplete BETWEEN DATEADD(month, DATEDIFF(month, 0, @DateTo), 0) AND @DateTo 
    AND (AC95.OnOffSite = 'ON' OR AC95.OnOffSiteSvc = 'ON') 
    AND 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) AS HoursMonth, 
     ReportKey 
FROM cte_PreResult 
GROUP BY [Period], 
     ZoneID, 
     ZoneName, 
     RegionCode, 
     RegionName, 
     BranchID, 
     BranchName, 
     ServicingRep, 
     RepName, 
     ReportKey 
+0

虽然这工作我一直在寻找这样做主要查询体内,因为这是一个大的查询的提取物。那可能吗。 – 2014-11-06 12:40:46

+0

当然只是添加区 SUM(DISTINCT AC95.PrepTime + AC95.SvcPrepTime + AC95.TravelTime + AC95.SvcTravelTime + AC95.VisitTime + AC95.SvcVisitTime)AS HoursMonth, – 2014-11-06 12:52:06

+0

非常感谢。 – 2014-11-06 12:53:40