我有两个查询,我需要进行一个查询,但其中只有一个条件不同。
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]
答案 0 :(得分:3)
使用条件聚合:从同一张表查询时:
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]
答案 1 :(得分:2)
将两个IN子句合并到单个IN子句中,并在SUM的计算中应用CASE逻辑,以便分别计算付款额和费用额。
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]
答案 2 :(得分:-1)
您可以同时加入两个查询
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