Reputation: 135
I got two queries where i need to make a single query , only one conditions are different among them .
SELECT DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate) AS [WeekEndDate],
LocationGroupname,
SUM(ISNULL(ChargeAmount,0)) AS CA
from [TransactionMasterReport] as tm where tm.PostDate between '01/01/2018' and '02/03/2018'
AND MODALITY IN ('DRUGS','E & M CODES','SPP & LASH')
**and transactiontype IN ('Charges' ,'Voided Charges')**
and locationgroupname like '%ABC%'
group by DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate),LocationGroupname
order by [WeekEndDate]
SELECT DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate) AS [WeekEndDate],
LocationGroupname,
SUM(ISNULL(PaymentAmount,0)) AS PA
from [TransactionMasterReport] as tm
where tm.PostDate between '01/01/2018' and '02/03/2018'
AND MODALITY IN ('DRUGS','E & M','SPP & LASH')
**and [TransactionType] in ('Payments','Adjustments')**
and locationgroupname like '%ABC%'
group by DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate),LocationGroupname
order by [WeekEndDate]
Upvotes: 0
Views: 60
Reputation: 31993
you can join your both query
with cte1 as
(
SELECT DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate) AS [WeekEndDate],
LocationGroupname,
SUM(ISNULL(ChargeAmount,0)) AS CA
from [TransactionMasterReport] as tm where tm.PostDate between '01/01/2018' and '02/03/2018'
AND MODALITY IN ('DRUGS','E & M CODES','SPP & LASH')
**and transactiontype IN ('Charges' ,'Voided Charges')**
and locationgroupname like '%ABC%'
group by DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate),LocationGroupname
order by [WeekEndDate]
), cte2 as(
SELECT DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate) AS [WeekEndDate],
LocationGroupname,
SUM(ISNULL(PaymentAmount,0)) AS PA
from [TransactionMasterReport] as tm
where tm.PostDate between '01/01/2018' and '02/03/2018'
AND MODALITY IN ('DRUGS','E & M','SPP & LASH')
**and [TransactionType] in ('Payments','Adjustments')**
and locationgroupname like '%ABC%'
group by DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate),LocationGroupname
order by [WeekEndDate]
) select cte1.* ,cte2.PA from cte1 join cte2 on cte1.LocationGroupname=cte2.LocationGroupname and cte1.WeekEndDate=cte2.WeekEndDate
Upvotes: -1
Reputation: 12959
Join two IN clauses into a single IN clause and apply CASE logic in the calculation of SUM, so that you are calculating paymentamount and chargeamount separately.
SELECT DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate) AS [WeekEndDate],
LocationGroupname,
SUM(CASE WHEN transactiontype IN (transactiontype IN ('Charges' ,'Voided Charges') THEN ISNULL(ChargeAmount,0) ELSE 0 END) AS CA,
SUM(CASE WHEN transactiontype IN (transactiontype IN ('Payments','Adjustments') THEN iSNULL(PaymentAmount,0) ELSE 0 END) AS PA
from [TransactionMasterReport] as tm where tm.PostDate between '01/01/2018' and '02/03/2018'
AND MODALITY IN ('DRUGS','E & M CODES','SPP & LASH')
and transactiontype IN ('Charges' ,'Voided Charges', 'Payments','Adjustments')
and locationgroupname like '%ABC%'
group by DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate),LocationGroupname
order by [WeekEndDate]
Upvotes: 2
Reputation: 37473
Use conditional aggregation: As you are querying from same table:
SELECT DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate) AS [WeekEndDate],
LocationGroupname,
SUM(case when transactiontype IN ('Charges' ,'Voided Charges') then ChargeAmount else 0 end) AS CA,
SUM(case when transactiontype IN ('Payments','Adjustments') then PaymentAmount else 0 end) AS PA
from [TransactionMasterReport] as tm where tm.PostDate between '01/01/2018' and '02/03/2018'
AND MODALITY IN ('DRUGS','E & M CODES','SPP & LASH')
and locationgroupname like '%ABC%'
group by DATEADD(DD,7-CHOOSE(DATEPART(dw, TM.PostDate), 2,3,4,5,6,7,1),TM.PostDate),LocationGroupname
order by [WeekEndDate]
Upvotes: 3