英文:
SQL - How to find a set of records that does NOT include a certain column value
问题
我正在处理医疗索赔数据,并需要帮助验证一个假设。
我想要找到所有特定药物的索赔,但这些索赔未使用适当的诊断代码进行计费。
比如说,我有一种名为Drug-X的药物,只有在存在诊断代码H99的情况下才能计费(在下面的示例中,Claim 123是有效的,因为它有正确的代码)。我想要找到使用Drug-X进行计费但根本没有诊断代码H99的索赔。
此外,ICD10代码可以附加到任何服务的任何行上(或所有服务)。
在下面的示例中,我希望能够只返回Claim ID为321的记录。
索赔ID | 个人ID | 服务 | ICD10代码 |
---|---|---|---|
123 | 555 | 注射 | H99 |
123 | 555 | Drug-X | |
123 | 555 | 检查 | |
123 | 555 | 诊断 | |
321 | 556 | 注射 | H88 |
321 | 556 | Drug-X | |
321 | 556 | 检查 | |
321 | 556 | 诊断 |
我尝试使用一些"where in (select distinct Claim ID)"和Exists
函数,但它们似乎不起作用。
英文:
I am working with healthcare claims data and I need assistance proving out a hypothesis.
I want to find all claims of a particular drug that were not billed with the appropriate diagnosis code.
So say I have Drug-X that can only be billed if there is a diagnosis code of H99 (in the below Claim 123 is valid because it has the right code), I want to find claims that were billed with Drug-X but did NOT have a diagnosis code of H99 at all.
Also, the ICD10 Code can be attached to any row for a service (or all services) of that claim
In the below example: I'd like to be able to return just the records where the Claimd ID is 321
Claim ID | Person ID | Service | ICD10 Code |
---|---|---|---|
123 | 555 | Injection | H99 |
123 | 555 | Drug-X | |
123 | 555 | Exam | |
123 | 555 | Diagnosis | |
321 | 556 | Injection | H88 |
321 | 556 | Drug-X | |
321 | 556 | Exam | |
321 | 556 | Diagnosis |
I have tried using some where in (select distinct Claim ID) and Exists
functions but they don't seem to work.
答案1
得分: 1
这可以通过使用group by
和having
子句来完成,条件是获取至少有一个Drug-X且没有H99代码的索赔:
select t.*
from mytable t
inner join (
select Claim_ID
from mytable
group by Claim_ID
having count(case when Service = 'Drug-X' then 1 end) > 0
and count(case when ICD10_Code = 'H99' then 1 end) = 0
) as s on s.Claim_ID = t.Claim_ID
英文:
This can be done using group by
and having
clause, the condition is getting Claims having at least one Drug-X and none of H99 Code :
select t.*
from mytable t
inner join (
select Claim_ID
from mytable
group by Claim_ID
having count(case when Service = 'Drug-X' then 1 end) > 0
and count(case when ICD10_Code = 'H99' then 1 end) = 0
) as s on s.Claim_ID = t.Claim_ID
答案2
得分: 0
使用窗口函数而不是自连接可能更高效。
选择t.*
从 (
选择*,
count_DrugX = 计数(情况当服务 = '药物-X'然后1结束) 在(Claim_ID)上分区,
count_H99 = 计数(情况当ICD10_Code = 'H99'然后1结束) 在(Claim_ID)上分区
从 mytable t
) t
其中 count_DrugX > 0
和 count_H99 = 0;
英文:
It's probably more efficient to do this using window functions, rather than a self-join.
select t.*
from (
select *,
count_DrugX = count(case when Service = 'Drug-X' then 1 end) over (partition by Claim_ID),
count_H99 = count(case when ICD10_Code = 'H99' then 1 end) over (partition by Claim_ID)
from mytable t
) t
where count_DrugX > 0
and count_H99 = 0;
</details>
# 答案3
**得分**: 0
以下是使用`exists`的解决方案:
```sql
with yourData as (
select *
from (values
('123', '555', 'Injection', 'H99'),
('123', '555', 'Drug-X', NULL),
('123', '555', 'Exam', NULL),
('123', '555', 'Diagnosis', NULL),
('321', '556', 'Injection', 'H88'),
('321', '556', 'Drug-X', NULL),
('321', '556', 'Exam', NULL),
('321', '556', 'Diagnosis', NULL)
) as x(ClaimID, PersonID, [Service], [IDC10])
), badClaims as (
select ClaimID
from yourData
where [Service] = 'Drug-X'
and not exists (
select *
from yourData as t
where yourData.ClaimID = t.ClaimID
and IDC10 = 'H99'
)
)
select *
from yourData
join badClaims
on badClaims.ClaimID = yourData.ClaimID;
将其分解,CTE(Common Table Expression) badClaims 寻找具有 Drug-X 的项目,并且在同一索赔中没有任何项目的 IDC10 = H99。一旦找到这样的索赔,我们返回该索赔的所有项目。
英文:
Here's a solution that uses exists
:
with yourData as (
select *
from (values
('123', '555', 'Injection', 'H99'),
('123', '555', 'Drug-X', NULL),
('123', '555', 'Exam',NULL),
('123', '555', 'Diagnosis', NULL),
('321', '556', 'Injection', 'H88'),
('321', '556', 'Drug-X', NULL),
('321', '556', 'Exam', NULL),
('321', '556', 'Diagnosis', NULL)
) as x(ClaimID, PersonID, [Service], [IDC10])
), badClaims as (
select ClaimID
from yourData
where [Service] = 'Drug-X'
and not exists (
select *
from yourData as t
where yourData.ClaimID = t.ClaimID
and IDC10 = 'H99'
)
)
select *
from yourData
join badClaims
on badClaims.ClaimID = yourData.ClaimID;
Breaking it down, the cte badClaims looks for line items that have Drug-X and no line item in the same claim has IDC10 = H99. Once such claims are found, we return all line items for that claim.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论