Manish Patel
Manish Patel

Reputation: 77

inner join results of "with" clause

I have 2 with clauses like this:

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; 

and also:

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; 

Now I want to join the results of the both of the outputs. How can I do it?

Edited: Added the <= cluase also.

Reults from my first T:

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

Results from my T2

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

The output i require:

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

Upvotes: 1

Views: 23769

Answers (3)

Manish Patel
Manish Patel

Reputation: 77

I was thinking it wrongly. Thanks for the help. This is how i achieved what exactly i want:

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;

Upvotes: 0

Martin Smith
Martin Smith

Reputation: 453406

You don't need a join. You can use

SELECT SUM(tfspm.PaymentOn) AS 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 BETWEEN '2013-05-01 12:00:00.000' 
                                 AND '2013-05-07 12:00:00.000'
              OR tfspm.PaymentOn BETWEEN '2013-05-08 12:00:00.000' 
                                     AND '2013-05-14 12:00:00.000' )
GROUP  BY tfsp.campaignID,
          cc.Name 

Upvotes: 2

DrCopyPaste
DrCopyPaste

Reputation: 4117

If I am right, after a WITH-clause you have to immediatly select the results of that afterwards. So IMHO your best try to achieve joining the both would be to save each of them into a temporary table and then join the contents of those two together.

UPDATE: after re-reading your question I realized that you probably don't want a (SQL-) join but just your 2 results packed together in one, so you could easily achieve that with what I descibed above, just select the contents of both temporary tables and put a UNION inbetween them.

Upvotes: 1

Related Questions