I am trying to join three tables in Snowflake, but getting an error on a third one, and not sure if I used a good key :
select
a.CALENDAR_DATE
, a.SALES_DOCUMENT
, a.DOCUMENT_NUMBER_CROSS_BORDER
, *bAUBEL
, *b.NETWR_ship*
, a.Delivery_time
, a.Route_label
, ue.order_ID
from DWHM_MALLGROUP.DM_LOG.SHIPMENTS a
left join (
select
AUBEL,
sum(NETWR) as NETWR_ship
from DWHM_MALLGROUP.SRC_SAP_ERP."2LIS_13_VDITM"
where MATNR in ('10027', '10200', '10402', '10500', '10602', '10603', '10650', '10700')
group by AUBEL
) b
on a.DOCUMENT_NUMBER_CROSS_BORDER = b.AUBEL
where
a.CALENDAR_DATE >= '2020-04-01'
and a.ROUTE in ('CZSK12', 'CZSK21', 'CZSK22', 'CZSK23', 'CZSK24', 'CZSK25', 'CZSK26', 'CZSK27', 'CZSK28', 'CZSK29', 'CZSK30', 'CZSK32', 'CZSK33', 'CZSK34', 'CZSK51', 'CZSK53', 'CZSK54', 'CZSK56')
group by a.CALENDAR_DATE, a.SALES_DOCUMENT, a.DOCUMENT_NUMBER_CROSS_BORDER, b.AUBEL, b.NETWR_ship, a.delivery_time, a.Route_LABEL
left join "DWHM_MALLGROUP"."DM_COMMERCIAL"."UNIT_ECONOMICS" as ue
ON a.SALES_DOCUMENT = ue.order_id
where ue.country = 'SI' and ue.delivery_date >= '2019-08-01'
Your 2nd LEFT JOIN should be moved up to before the WHERE clause:
select
a.CALENDAR_DATE
, a.SALES_DOCUMENT
, a.DOCUMENT_NUMBER_CROSS_BORDER
, b.AUBEL
, b.NETWR_ship
, a.Delivery_time
, a.Route_label
, ue.order_ID
from DWHM_MALLGROUP.DM_LOG.SHIPMENTS a
left join (
select
AUBEL,
sum(NETWR) as NETWR_ship
from DWHM_MALLGROUP.SRC_SAP_ERP."2LIS_13_VDITM"
where MATNR in ('10027', '10200', '10402', '10500', '10602', '10603', '10650', '10700')
group by AUBEL
) b
on a.DOCUMENT_NUMBER_CROSS_BORDER = b.AUBEL
left join "DWHM_MALLGROUP"."DM_COMMERCIAL"."UNIT_ECONOMICS" as ue
ON a.SALES_DOCUMENT = ue.order_id
where
a.CALENDAR_DATE >= '2020-04-01'
and a.ROUTE in ('CZSK12', 'CZSK21', 'CZSK22', 'CZSK23', 'CZSK24', 'CZSK25', 'CZSK26', 'CZSK27', 'CZSK28', 'CZSK29', 'CZSK30', 'CZSK32', 'CZSK33', 'CZSK34', 'CZSK51', 'CZSK53', 'CZSK54', 'CZSK56')
and ue.country = 'SI' and ue.delivery_date >= '2019-08-01'
group by a.CALENDAR_DATE, a.SALES_DOCUMENT, a.DOCUMENT_NUMBER_CROSS_BORDER, b.AUBEL, b.NETWR_ship, a.delivery_time, a.Route_LABEL
NOTE: I am not familiar with the *b that you have in the SELECT clause... it looks like another typo to me, so I removed it.
If you are trying to perform the aggregate (GROUP BY) before the 2nd LEFT JOIN, then you can use a CTE:
WITH CTE_1 AS (
select
a.CALENDAR_DATE
, a.SALES_DOCUMENT
, a.DOCUMENT_NUMBER_CROSS_BORDER
, b.AUBEL
, b.NETWR_ship
, a.Delivery_time
, a.Route_label
from DWHM_MALLGROUP.DM_LOG.SHIPMENTS a
left join (
select
AUBEL,
sum(NETWR) as NETWR_ship
from DWHM_MALLGROUP.SRC_SAP_ERP."2LIS_13_VDITM"
where MATNR in ('10027', '10200', '10402', '10500', '10602', '10603', '10650', '10700')
group by AUBEL
) b
on a.DOCUMENT_NUMBER_CROSS_BORDER = b.AUBEL
where
a.CALENDAR_DATE >= '2020-04-01'
and a.ROUTE in ('CZSK12', 'CZSK21', 'CZSK22', 'CZSK23', 'CZSK24', 'CZSK25', 'CZSK26', 'CZSK27', 'CZSK28', 'CZSK29', 'CZSK30', 'CZSK32', 'CZSK33', 'CZSK34', 'CZSK51', 'CZSK53', 'CZSK54', 'CZSK56')
group by a.CALENDAR_DATE, a.SALES_DOCUMENT, a.DOCUMENT_NUMBER_CROSS_BORDER, b.AUBEL, b.NETWR_ship, a.delivery_time, a.Route_LABEL
) c1
select
c1.CALENDAR_DATE
, c1.SALES_DOCUMENT
, c1.DOCUMENT_NUMBER_CROSS_BORDER
, c1.AUBEL
, c1.NETWR_ship
, c1.Delivery_time
, c1.Route_label
, ue.order_ID
left join "DWHM_MALLGROUP"."DM_COMMERCIAL"."UNIT_ECONOMICS" as ue
ON c1.SALES_DOCUMENT = ue.order_id
and ue.country = 'SI' and ue.delivery_date >= '2019-08-01'
This can (and should, IMO) be cleaned up some more, but I am trying to address the question you asked about joining 3 tables.
There are multiple issues with the SQL.
, *bAUBEL
, *b.NETWR_ship*
we only use * for selecting all fields from a table. For example select A.* , B.col1 from Table_a A , Table_B
sequence for join is
SELECT
FROM Table
JOIN other Table
JOIN Condition
WHERE
GROUP BY
HAVING
Try this SQL
SELECT
a.calendar_date,
a.sales_document,
a.document_number_cross_border,
b.aubel,
b.netwr_ship,
a.delivery_time,
a.route_label,
ue.order_id
FROM
dwhm_mallgroup.dm_log.SHIPMENTS a
left join (
select
AUBEL,
sum(NETWR) as NETWR_ship
from DWHM_MALLGROUP.SRC_SAP_ERP."2LIS_13_VDITM"
where MATNR in ('10027', '10200', '10402', '10500', '10602', '10603', '10650', '10700')
group by AUBEL
) b
on a.DOCUMENT_NUMBER_CROSS_BORDER = b.AUBEL
left join "DWHM_MALLGROUP"."DM_COMMERCIAL"."UNIT_ECONOMICS" as ue ON a.sales_document = ue.order_id
WHERE
a.calendar_date >= '2020-04-01'
AND a.route IN (
'CZSK12',
'CZSK21',
'CZSK22',
'CZSK23',
'CZSK24',
'CZSK25',
'CZSK26',
'CZSK27',
'CZSK28',
'CZSK29',
'CZSK30',
'CZSK32',
'CZSK33',
'CZSK34',
'CZSK51',
'CZSK53',
'CZSK54',
'CZSK56'
)
AND ue.country = 'SI'
AND ue.delivery_date >= '2019-08-01'
GROUP BY
a.calendar_date,
a.sales_document,
a.document_number_cross_border,
b.aubel,
b.netwr_ship,
a.delivery_time,
a.route_label
ue.order_id
Related
I do have a problem on how to insert numerous rows of data into a table from an existing table with conditions. So this first code is how I created my table.
CREATE TABLE MARKETING_COMMODITY
AS(
SELECT A.DTIME_SIGNATURE
, A.AMT_SIGNED
, A.CNT_SIGNED
, A.APPLICATION_AMOUNT
, A.PRODUCT
, A.PRODUCT_TYPE
, A.PRODUCT_PRICE
, VSR.NAME_PRODUCER
, vsr.text_model_number
, vsp.partner_name
, vsp.partner_brand
, vspl.salesroom
, vspl.mall
FROM DM_SALES.V_SALES_DM_DATA A
LEFT JOIN DM_SALES.V_SALES_DM_PARTNER VSP ON A.CODE_SALESROOM_PARTNER = VSP.CODE_SALESROOM_PARTNER
LEFT JOIN dm_sales.v_sales_dm_pos_list vspl on a.code_salesroom = vspl.code_salesroom
LEFT JOIN DM_SALES.V_SALES_DM_CONTRACT_BUNDLE VSR ON A.CONTRACT_NUMBER = VSR.CONTRACT_NUMBER
WHERE 1=1
AND a.contract_state <> 'Cancelled'
AND a.cnt_signed=1
AND A.LOAN_TYPE = 'Consumer Loan'
AND (TRUNC(A.DTIME_SIGNATURE) BETWEEN DATE'2022-01-01' AND DATE'2022-08-31')
;
And this is how I'd like to insert my new rows (its like updating the table to get the new data up to the current day)
INSERT INTO MARKETING_COMMODITY
VALUES(
SELECT A.DTIME_SIGNATURE
, A.AMT_SIGNED
, A.CNT_SIGNED
, A.APPLICATION_AMOUNT
, A.PRODUCT
, A.PRODUCT_TYPE
, A.PRODUCT_PRICE
, VSR.NAME_PRODUCER
, vsr.text_model_number
, vsp.partner_name
, vsp.partner_brand
, vspl.salesroom
, vspl.mall
FROM DM_SALES.V_SALES_DM_DATA A
LEFT JOIN DM_SALES.V_SALES_DM_PARTNER VSP ON A.CODE_SALESROOM_PARTNER = VSP.CODE_SALESROOM_PARTNER
LEFT JOIN dm_sales.v_sales_dm_pos_list vspl on a.code_salesroom = vspl.code_salesroom
LEFT JOIN DM_SALES.V_SALES_DM_CONTRACT_BUNDLE VSR ON A.CONTRACT_NUMBER = VSR.CONTRACT_NUMBER
WHERE 1=1
AND a.contract_state <> 'Cancelled'
AND a.cnt_signed=1
AND A.LOAN_TYPE = 'Consumer Loan'
AND (TRUNC(A.DTIME_SIGNATURE) BETWEEN DATE'2022-09-01' AND DATE'2022-09-10')
;
What can you suggest? Thanks!
Don't confuse the syntax for "CREATE TABLE AS SELECT" with the syntax for "INSERT INTO SELECT". Explicitely add the columns you're inserting into and remove the "AS" keyword. This should work:
INSERT INTO MARKETING_COMMODITY
(
dtime_signature
,amt_signed
,cnt_signed
,application_amount
,product
,product_type
,product_price
,name_producer
,text_model_number
,partner_name
,partner_brand
,salesroom
,mall
)
SELECT A.DTIME_SIGNATURE
, A.AMT_SIGNED
, A.CNT_SIGNED
, A.APPLICATION_AMOUNT
, A.PRODUCT
, A.PRODUCT_TYPE
, A.PRODUCT_PRICE
, VSR.NAME_PRODUCER
, vsr.text_model_number
, vsp.partner_name
, vsp.partner_brand
, vspl.salesroom
, vspl.mall
FROM DM_SALES.V_SALES_DM_DATA A
LEFT JOIN DM_SALES.V_SALES_DM_PARTNER VSP ON A.CODE_SALESROOM_PARTNER = VSP.CODE_SALESROOM_PARTNER
LEFT JOIN dm_sales.v_sales_dm_pos_list vspl on a.code_salesroom = vspl.code_salesroom
LEFT JOIN DM_SALES.V_SALES_DM_CONTRACT_BUNDLE VSR ON A.CONTRACT_NUMBER = VSR.CONTRACT_NUMBER
WHERE 1=1
AND a.contract_state <> 'Cancelled'
AND a.cnt_signed=1
AND A.LOAN_TYPE = 'Consumer Loan'
AND (TRUNC(A.DTIME_SIGNATURE) BETWEEN DATE'2022-09-01' AND DATE'2022-09-10')
;
I have the following CTE inside a view and I would like to add some NULLIF (or equivalent) function so that If WTHD_BOX16 is NULL then replace with an empty string (blank), otherwise display the value.
WITH VWDETAIL AS
( SELECT VENDOR_ID , 'PA/13679089' AS WTHD_BOX17 , WTHD_BASIS_AMT AS WTHD_BOX18 , YEAR(PYMNT_DT) AS YEAR ,
CASE WHEN PYMNT_DT <= '01/31/2014' THEN '1099' ELSE 'NGC' END AS WTHD_CNTL_ID
FROM PS_WTHD_TRXN_TBL A
WHERE WTHD_CLASS IN ('01','02','07')
AND BUSINESS_UNIT IN ('10000','50000')
AND PYMNT_DT <= CASE BUSINESS_UNIT WHEN '10000' THEN '01/31/2014' ELSE '12/31/2018' END)
, BOX16DATA AS
( SELECT --A.BUSINESS_UNIT, A.VOUCHER_ID,
A.PYMNT_TYPE, SUM(A.PAID_AMT) AS BOX16PAIDAMT, SUM(A.PYMNT_GROSS_AMT) AS WTHD_BOX16, B.VENDOR_ID, YEAR(A.SCHEDULED_PAY_DT) AS 'Year'
FROM PS_PYMNT_VCHR_XREF A
INNER JOIN PS_VOUCHER B ON B.BUSINESS_UNIT = A.BUSINESS_UNIT AND B.VOUCHER_ID = A.VOUCHER_ID
WHERE PYMNT_TYPE = 'W'
AND REMIT_VENDOR = '47860A'
GROUP BY B.VENDOR_ID, A.PYMNT_TYPE, YEAR(A.SCHEDULED_PAY_DT)
)
SELECT A.VENDOR_ID, B.WTHD_BOX16, WTHD_BOX17 , SUM(WTHD_BOX18) AS WTHD_BOX18 , YEAR , WTHD_CNTL_ID
FROM VWDETAIL A
LEFT OUTER JOIN BOX16DATA B ON B.VENDOR_ID = A.VENDOR_ID AND B.Year = YEAR
GROUP BY A.VENDOR_ID , WTHD_BOX17 , YEAR , WTHD_CNTL_ID, B.WTHD_BOX16
ORDER BY A.YEAR
GO
I added the NULLIF to the final Select statement, however I am getting back the error 'Error converting data type varchar to numeric'.
SELECT A.VENDOR_ID, NULLIF(B.WTHD_BOX16, ''), WTHD_BOX17 , SUM(WTHD_BOX18) AS WTHD_BOX18 , YEAR , WTHD_CNTL_ID
FROM VWDETAIL A
LEFT OUTER JOIN BOX16DATA B ON B.VENDOR_ID = A.VENDOR_ID AND B.Year = YEAR
GROUP BY A.VENDOR_ID , WTHD_BOX17 , YEAR , WTHD_CNTL_ID, B.WTHD_BOX16
ORDER BY A.YEAR
GO
How can I fix this so that I will have the numeric values if there isn't a NULL and an empty string for a NULL row? Using MS SQL Server.
you can cast the columns to varchar e.g. cast(col as varchar(50)). blank cannot be converted to numeric.
Below is my SQL query. If the fbkWeightMaster has multiple records for the same patient, it returns multiple rows in the result. I want to avoid it. I tried using Distinct, but did not work.
I confirmed that the issue occurs when there are multiple entries in fbkWeightMaster table from the below query.
Select Distinct
a.patientid
, a.firstname
, a.lastname
, a.mobile
, a.dob
, b.InitialWeight
, b.LatestWeight
, a.height
, c.PlanName
, a.warning
, a.indicator
, a.city
, a.email
, a.state
, a.comments
, a.introduction
, a.gender
, a.address
, a.landline
, a.FoodPreference
From
fbkPatientMaster a
Join
fbkPlanMaster c On a.PlanID = c.PlanID
Join
fbkWeightMaster b On a.PatientID = b.PatientID
Join
fbkChartMaster d On d.PatientID = a.PatientID
Where
IsActive = 1
And a.PatientID Not In (Select Distinct PatientID
From fbkChartMaster
Where Cast(ChartDate As Date) = DateAdd(Day, 1, Cast(GetDate() As Date)))
And a.PatientID Not In (Select Distinct PatientID
From fbkChartHold
Where Cast(ChartHoldTo As Date) > Cast(GetDate() As Date));
You could change your join to a cross apply() and select top 1 from fbkWeightMaster according to some order, such as a date column. This will return only 1 row from fbkWeightMaster per a.patientid.
cross apply() works similar to an inner join if you want the functionality you get with a left join use outer apply() instead.
Select
a.patientid
, a.firstname
, a.lastname
, a.mobile
, a.dob
, b.InitialWeight
, b.LatestWeight
, a.height
, c.PlanName
, a.warning
, a.indicator
, a.city
, a.email
, a.state
, a.comments
, a.introduction
, a.gender
, a.address
, a.landline
, a.FoodPreference
From
fbkPatientMaster a
Join
fbkPlanMaster c On a.PlanID = c.PlanID
Join
fbkChartMaster d On d.PatientID = a.PatientID
cross apply (
select top 1 *
from fbkWeightMaster b
where a.PatientID = b.PatientID
order by b.date desc
) b
Where
IsActive = 1
And a.PatientID Not In (Select Distinct PatientID
From fbkChartMaster
Where Cast(ChartDate As Date) = DateAdd(Day, 1, Cast(GetDate() As Date)))
And a.PatientID Not In (Select Distinct PatientID
From fbkChartHold
Where Cast(ChartHoldTo As Date) > Cast(GetDate() As Date));
I have the following queries:
select AccountId
into #liveCustomers
from AccountExtensionBase where New_duos_group not in ('T053','T054')
and New_AccountStage = 7
select AccountId
into #customerWhoLeft
from New_marketmessagein as a
inner join AccountExtensionBase as b on a.new_accountmminid = b.AccountId
where New_MessageTypeCode = '105L'
and a.New_EffectiveFromDate > '30 jun 2016'
and b.New_duos_group not in ('T053','T054')
select
accountid
, New_MPRNNumber
, New_duos_group
, New_CommercialAgreementDayRate
, New_CommercialAgreementNightRate
, New_CommercialAgreementHeatRate
, New_Tariffpriceagreedatsignup
, New_Tariffname
into
#monthCustomers
from
AccountExtensionBase
where
AccountId in (select * from #customerWhoLeft)
or
AccountId in (select * from #liveCustomers)
I now wish to join a table called usagefactorExtensionBase and join only the row containing the most recent read date but when I try to join this to my table of 4985 monthly customers I get like 106,813 rows using this code so I think my join or methodology has gone awry, can someone please help me correct the error so I display the list of monthCustomers plus the read details of their most recent read.
Attempting:
select
accountid
, New_MPRNNumber
, New_duos_group
, New_CommercialAgreementDayRate
, New_CommercialAgreementNightRate
, New_CommercialAgreementHeatRate
, New_Tariffpriceagreedatsignup
, New_Tariffname
, max(b.New_EffectiveFromDate)
, b.New_ActualUsageFactor
, b.New_EstimatedUseage
from
#monthCustomers as a
left join
New_marketmessageinusagefactorExtensionBase as b
on a.AccountId = b.new_accountmmusagefactorid
group by
accountid
, New_MPRNNumber
, New_duos_group
, New_CommercialAgreementDayRate
, New_CommercialAgreementNightRate
, New_CommercialAgreementHeatRate
, New_Tariffpriceagreedatsignup
, New_Tariffname
, b.New_ActualUsageFactor
, b.New_EstimatedUseage
try this,
SELECT
accountid,
New_MPRNNumber,
New_duos_group,
New_CommercialAgreementDayRate,
New_CommercialAgreementNightRate,
New_CommercialAgreementHeatRate,
New_Tariffpriceagreedatsignup,
New_Tariffname,
b.New_EffectiveFromDate,
b.New_ActualUsageFactor,
b.New_EstimatedUseage
FROM #monthCustomers AS a
-- Get only max date rows for each AccountID
LEFT JOIN( SELECT t1.*
FROM New_marketmessageinusagefactorExtensionBase AS t1
INNER JOIN ( SELECT new_accountmmusagefactorid, MAX(New_EffectiveFromDate) AS New_EffectiveFromDate_Max
FROM New_marketmessageinusagefactorExtensionBase
GROUP BY new_accountmmusagefactorid
) AS t2 ON t2.new_accountmmusagefactorid = t1.new_accountmmusagefactorid
AND t2.New_EffectiveFromDate_Max = t1.New_EffectiveFromDate
)AS b
ON a.AccountId = b.new_accountmmusagefactorid
there might be rows with same date, try below if is works,
SELECT
accountid,
New_MPRNNumber,
New_duos_group,
New_CommercialAgreementDayRate,
New_CommercialAgreementNightRate,
New_CommercialAgreementHeatRate,
New_Tariffpriceagreedatsignup,
New_Tariffname,
b.New_EffectiveFromDate,
b.New_ActualUsageFactor,
b.New_EstimatedUseage
FROM #monthCustomers AS a
-- Get only max date rows for each AccountID
LEFT JOIN( SELECT New_MPRNNumber,
New_duos_group,
New_CommercialAgreementDayRate,
New_CommercialAgreementNightRate,
New_CommercialAgreementHeatRate,
New_Tariffpriceagreedatsignup,
New_Tariffname,
MAX(New_EffectiveFromDate) AS New_EffectiveFromDate,
New_ActualUsageFactor,
New_EstimatedUseage
FROM New_marketmessageinusagefactorExtensionBase AS t1
GROUP BY
New_MPRNNumber,
New_duos_group,
New_CommercialAgreementDayRate,
New_CommercialAgreementNightRate,
New_CommercialAgreementHeatRate,
New_Tariffpriceagreedatsignup,
New_Tariffname,
New_ActualUsageFactor,
New_EstimatedUseage
)AS b
ON a.AccountId = b.new_accountmmusagefactorid
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
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
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.
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;