1

我的 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 Middle School 列,所以最终结果应该看起来像

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

有没有办法做到这一点?

4

3 回答 3

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
于 2013-03-22T16:08:37.930 回答
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
于 2013-03-22T16:25:50.943 回答
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上的简单演示

于 2013-03-22T16:34:15.940 回答