1

私は次のような句を2つ持っています:

WITH T
     AS (SELECT tfsp.SubmissionID,
                tfsp.Amount,
                tfsp.campaignID,
                cc.Name
         FROM   tbl_FormSubmissions_PaymentsMade tfspm
                INNER JOIN tbl_FormSubmissions_Payment tfsp
                  ON tfspm.SubmissionID = tfsp.SubmissionID
                INNER JOIN tbl_CurrentCampaigns cc
                  ON tfsp.CampaignID = cc.ID
         WHERE  tfspm.isApproved = 'True'
                AND tfspm.PaymentOn >= '2013-05-01 12:00:00.000' AND tfspm.PaymentOn <= '2013-05-07 12:00:00.000')
SELECT SUM(Amount) AS TotalAmount,
       campaignID,
       Name
FROM   T
GROUP  BY campaignID,
          Name; 

また:

WITH T1
     AS (SELECT tfsp.SubmissionID,
                tfsp.Amount,
                tfsp.campaignID,
                cc.Name
         FROM   tbl_FormSubmissions_PaymentsMade tfspm
                INNER JOIN tbl_FormSubmissions_Payment tfsp
                  ON tfspm.SubmissionID = tfsp.SubmissionID
                INNER JOIN tbl_CurrentCampaigns cc
                  ON tfsp.CampaignID = cc.ID
         WHERE  tfspm.isApproved = 'True'
                AND tfspm.PaymentOn >= '2013-05-08 12:00:00.000' AND tfspm.PaymentOn <= '2013-05-14 12:00:00.000')
SELECT SUM(Amount) AS TotalAmount,
       campaignID,
       Name
FROM   T1
GROUP  BY campaignID,
          Name; 

ここで、両方の出力の結果を結合したいと思います。どうすればいいですか?

編集: <= 節も追加。

私の最初の T からの結果:

Amount-----ID----Name
1000----- 2-----Annual Fund
83--------1-----Athletics Fund
300-------3-------Library Fund

T2 の結果

850-----2-------Annual Fund
370-----4-------Other

私が必要とする出力:

1800-----2------Annual Fund
83-------1------Athletics Fund
300------3-------Library Fund
370------4-----Other
4

3 に答える 3

0

私はそれを間違って考えていました。助けてくれてありがとう。これはまさに私が望むものを達成した方法です:

WITH 
T AS (
SELECT tfsp.SubmissionID , Amount1 =
CASE
WHEN tfspm.PaymentOn  >= '2013-01-10 11:34:54.000' AND tfspm.PaymentOn <= '2013-04-10 11:34:54.000' THEN tfsp.Amount
END
, Amount2 = 
CASE
WHEN tfspm.PaymentOn  >= '2013-05-01 11:34:54.000' AND tfspm.PaymentOn <= '2013-05-23 11:34:54.000' THEN tfsp.Amount
END
 , tfsp.campaignID , cc.Name FROM tbl_FormSubmissions_PaymentsMade tfspm 
INNER JOIN tbl_FormSubmissions_Payment tfsp ON tfspm.SubmissionID = tfsp.SubmissionID 
INNER JOIN tbl_CurrentCampaigns cc ON tfsp.CampaignID = cc.ID
WHERE tfspm.isApproved = 'True'   

)
SELECT ISNULL(SUM(Amount1),0) AS TotalAmount1, ISNULL(SUM(Amount2),0) AS TotalAmount2, campaignID , Name FROM T  GROUP BY campaignID, Name;
于 2013-05-23T15:07:18.293 に答える