SQL – 如何查找不包含特定列值的一组记录

huangapple go评论61阅读模式
英文:

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 byhaving子句来完成,条件是获取至少有一个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

Demo here

答案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
		(&#39;123&#39;, &#39;555&#39;, &#39;Injection&#39;, &#39;H99&#39;),
		(&#39;123&#39;, &#39;555&#39;, &#39;Drug-X&#39;, NULL),
		(&#39;123&#39;, &#39;555&#39;, &#39;Exam&#39;,NULL),
		(&#39;123&#39;, &#39;555&#39;, &#39;Diagnosis&#39;, NULL),
		(&#39;321&#39;, &#39;556&#39;, &#39;Injection&#39;, &#39;H88&#39;),
		(&#39;321&#39;, &#39;556&#39;, &#39;Drug-X&#39;, NULL),
		(&#39;321&#39;, &#39;556&#39;, &#39;Exam&#39;,  NULL),
		(&#39;321&#39;, &#39;556&#39;, &#39;Diagnosis&#39;,  NULL)
	) as x(ClaimID, PersonID, [Service], [IDC10])
), badClaims as (
	select ClaimID
	from yourData
	where [Service] = &#39;Drug-X&#39;
		and not exists (
			select *
			from yourData as t
			where yourData.ClaimID = t.ClaimID
				and IDC10 = &#39;H99&#39;
		)
)
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.

huangapple
  • 本文由 发表于 2023年7月14日 06:09:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定