将行中的数据与相同的键组合在一起

时间:2013-03-22 16:06:33

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

我的sql语句看起来像这样。

SELECT fe.AdmSiteID,A.SiteDescription, 
        '' AS SNAP, ''  AS TANF, '' AS Foster, ''   AS FDPIR, ''    AS Homeless, '' AS Migrant, ''  AS Runaway, 
        '' AS HeadStart, '' AS EvenStart, ''    AS DirectCertified,  '' AS NoApplication, '' AS FreeIncome, '' AS ReducedIncome, 
        ''  AS RefusedBenefits,''   AS DeniedIncome, '' AS DeniedVerification,  ''  AS DeniedNotValid, ''   AS TempFreeGrace, ''    AS TempReducedGrace, 
        ''  AS DeniedInactive, ''   AS DNQ, '' AS ExpiredGrace 
FROM FarEligibilityEnrollmentCounts fe 
inner join AdmSites A on fe.AdmSiteID = A.AdmSiteID 
GROUP BY fe.AdmSiteID,A.SiteDescription 
union
select fe.AdmSiteID,A.SiteDescription,sum(PatronCount) as SNAP, ''  AS TANF, '' AS Foster, ''   AS FDPIR, ''    AS Homeless, '' AS Migrant, ''  AS Runaway, 
        '' AS HeadStart, '' AS EvenStart, ''    AS DirectCertified,  '' AS NoApplication, '' AS FreeIncome, '' AS ReducedIncome, 
        ''  AS RefusedBenefits,''   AS DeniedIncome, '' AS DeniedVerification,  ''  AS DeniedNotValid, ''   AS TempFreeGrace, ''    AS TempReducedGrace, 
        ''  AS DeniedInactive, ''   AS DNQ, '' AS ExpiredGrace
        from FarEligibilityEnrollmentCounts fe 
        inner join AdmSites A on fe.AdmSiteID = A.AdmSiteID 
        where fe.FarStudentCaseID = '4FA4AED1-75C9-4CB4-9FF2-102C2D964CC1'
        Group BY fe.AdmSiteID,A.SiteDescription       
union 
select fe.AdmSiteID,A.SiteDescription,'' as SNAP, sum(PatronCount)  AS TANF, '' AS Foster, ''   AS FDPIR, ''    AS Homeless, '' AS Migrant, ''  AS Runaway, 
        '' AS HeadStart, '' AS EvenStart, ''    AS DirectCertified,  '' AS NoApplication, '' AS FreeIncome, '' AS ReducedIncome, 
        ''  AS RefusedBenefits,''   AS DeniedIncome, '' AS DeniedVerification,  ''  AS DeniedNotValid, ''   AS TempFreeGrace, ''    AS TempReducedGrace, 
        ''  AS DeniedInactive, ''   AS DNQ, '' AS ExpiredGrace
        from FarEligibilityEnrollmentCounts fe 
        inner join AdmSites A on fe.AdmSiteID = A.AdmSiteID 
        where fe.FarStudentCaseID = 'B348D33C-5C32-4CFE-B508-1E0CE2A9D428'
        Group BY fe.AdmSiteID,A.SiteDescription 

查询返回看起来像这样的数据。

AE1EF8E3-A093-410D-A02A-2B6DB334C758    Dowell Middle School    0       0
0359AF48-882F-4D54-B39D-341B17D64403    Evans Middle School     0       0
0359AF48-882F-4D54-B39D-341B17D64403    Evans Middle School     0       1595
0359AF48-882F-4D54-B39D-341B17D64403    Evans Middle School     1045    0

我正在尝试合并3个Evans中学专栏,因此最终结果应该是

AE1EF8E3-A093-410D-A02A-2B6DB334C758    Dowell Middle School    0       0
0359AF48-882F-4D54-B39D-341B17D64403    Evans Middle School     1045    1595

有办法做到这一点吗?

3 个答案:

答案 0 :(得分:2)

select  AdmSiteID
,       SiteDescription
,       sum(SNAP) as TotalSNAP
,       sum(TANF) as TotalTANF
,       ... Other Sums Here ...
from    (
        ...  Your Query With Unions Here ...
        ) as SubQueryAlias
group by
        AdmSiteID
,       SiteDescription

答案 1 :(得分:0)

DECLARE @tab TABLE(Guids VARCHAR(100),School VARCHAR(30),Snap INT,Tnaf INT)
INSERT INTO @tab
VALUES('AE1EF8E3-A093-410D-A02A-2B6DB334C758','Dowell Middle School',0,0)
  ,('0359AF48-882F-4D54-B39D-341B17D64403','Evans Middle School',0,0)
  ,('0359AF48-882F-4D54-B39D-341B17D64403','Evans Middle School',0,1595)
  ,('0359AF48-882F-4D54-B39D-341B17D64403','Evans Middle School',1045,0)

SELECT *
FROM @tab 

SELECT DISTINCT Guids
,School
      ,SUM(Snap) OVER(PARTITION BY Guids,School) AS TtlSnap
      ,SUM(Tnaf) OVER(PARTITION BY Guids,School) AS TtlTnaf
FROM @tab
ORDER BY School

答案 2 :(得分:0)

使用CTE

选项
 ;WITH cte AS
 (
  SELECT AdmSiteID, SiteDescription, sum(PatronCount) as SNAP, '' AS TANF
  FROM FarEligibilityEnrollmentCounts JOIN ...  
  GROUP BY AdmSiteID, SiteDescription 
  UNION 
  SELECT AdmSiteID, SiteDescription, '' as SNAP, sum(PatronCount)  AS TANF
  FROM FarEligibilityEnrollmentCounts JOIN ... 
  GROUP BY AdmSiteID, SiteDescription
  ) 
  SELECT AdmSiteID, SiteDescription, MAX(SNAP), MAX(TANF)
  FROM cte
  GROUP BY AdmSiteID, SiteDescription

此外,您可以在没有过多联合的情况下尝试此脚本

select fe.AdmSiteID,A.SiteDescription,
       ISNULL(SUM(CASE WHEN fe.FarStudentCaseID = '4FA4AED1-75C9-4CB4-9FF2-102C2D964CC1' THEN PatronCount END), 0) AS SNAP,
       ISNULL(SUM(CASE WHEN fe.FarStudentCaseID = 'B348D33C-5C32-4CFE-B508-1E0CE2A9D428' THEN PatronCount END), 0) AS TANF,
       '' AS Foster, ''   AS FDPIR, ''    AS Homeless, '' AS Migrant, ''  AS Runaway, 
       '' AS HeadStart, '' AS EvenStart, ''    AS DirectCertified,  '' AS NoApplication, '' AS FreeIncome, '' AS ReducedIncome, 
       '' AS RefusedBenefits,''   AS DeniedIncome, '' AS DeniedVerification,  ''  AS DeniedNotValid, ''   AS TempFreeGrace, ''    AS TempReducedGrace, 
       '' AS DeniedInactive, ''   AS DNQ, '' AS ExpiredGrace
from FarEligibilityEnrollmentCounts fe 
  inner join AdmSites A on fe.AdmSiteID = A.AdmSiteID       
Group BY fe.AdmSiteID,A.SiteDescription

SQLFiddle

上的简单演示