我被要求报告提供商当月收取的费用。当前的标准是一个代码意味着一个费用。我现在遇到的问题是,他们为其中一个提供程序添加了一个例外。其中,如果以F开头或以G结尾的代码在服务的同一天中有1位患者使用4或5个代码,则它们仅计为3个费用。
For example (Pv2 is the one with the special case)
Prov Patient ServiceDate Code
Pv1 Pt1 1/1/2019 99001
Pv1 Pt2 1/4/2019 99004
Pv1 Pt2 1/2/2019 F0001
Pv2 Pt1 1/1/2019 F0001
PV2 Pt1 1/1/2019 1000G
PV2 Pt1 1/1/2019 F0012
PV2 Pt1 1/1/2019 1003G
PV2 Pt1 1/1/2019 F1100
PV2 Pt2 1/4/2019 99001
报告应如下所示
Prov ChargeCount
Pv1 3
Pv2 4
这是我当前的代码:
Select Prov, Count(Code)
From Table
Group by Prov
所以我的输出
Prov ChargeCount
Pv1 3
Pv2 6
答案 0 :(得分:0)
这是可以满足您需要的功能代码,是我要采用的第一种方法。基本上,您将例外情况和标准案例分别进行总计,然后应用例外情况调整并将这两个值加在一起。
With DataGather as (
SELECT
Prov,
SUM(CASE
WHEN Code Like 'F%' Or Code Like '%G'
Then 1
Else 0
END
) as FGCodes,
SUM(CASE
WHEN WHEN Code Like 'F%' Or Code Like '%G'
Then 0
Else 1
END
) as Codes
FROM Table
GROUP BY Prov
)
SELECT Prov,
Codes +
CASE
WHEN FGCodes > 3
THEN 3
ELSE FGCodes
END as ChargeCount
FROM DataGather
编辑:
在附加条件下,最后一个CASE语句可以重写为:
CASE
WHEN FGCodes <= 3
THEN FGCodes
WHEN FGCodes <= 5
THEN 3
ELSE 5
END as ChargeCount
答案 1 :(得分:0)
您可以尝试使用以下CTE代码-
WITH CTE AS
(
SELECT Prov,Patient, 3 CNT
FROM your_table
WHERE YEAR(ServiceDate) = 2019
AND MONTH(ServiceDate) = 1
AND (
LEFT(Code,1) = 'F' OR RIGHT(Code,1) = 'G'
)
GROUP BY Prov,Patient
HAVING COUNT(Code) IN (4,5)
UNION ALL
SELECT Prov,Patient, COUNT(Code)
FROM your_table
WHERE YEAR(ServiceDate) = 2019
AND MONTH(ServiceDate) = 1
AND (
LEFT(Code,1) = 'F' OR RIGHT(Code,1) = 'G'
)
GROUP BY Prov,Patient
HAVING COUNT(Code) NOT IN (4,5)
UNION ALL
SELECT Prov,Patient, COUNT(Code)
FROM your_table
WHERE YEAR(ServiceDate) = 2019
AND MONTH(ServiceDate) = 1
AND LEFT(Code,1) <> 'F'
AND RIGHT(Code,1) <> 'G'
GROUP BY Prov,Patient
)
SELECT Prov, SUM(CNT) ChargeCount
FROM CTE
GROUP BY Prov
输出为-
Prov ChargeCount
Pv1 3
PV2 4