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

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 に答える
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 に答える