TSQL使用group by聚合多个列

时间:2014-11-06 12:13:04

标签: sql-server sql-server-2008 tsql aggregate

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

表格

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

注意本例中的2行

查询

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

结果

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

enter image description here

认为这可能是由于AC96中的多行,我删除了Group By,并显示了两行。这必然是SUM aggreagte使值加倍的原因。

enter image description here

问题

当AC96中有多行时,如何正确地将查询正确地指定为正确计算值而不会使数字加倍或更多?

1 个答案:

答案 0 :(得分: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