SQL选择多列中存在的多个值

时间:2018-01-09 17:03:34

标签: sql sql-server exists

我有两个表DiagnosisCodes和DiagnosisConditions,如下所示。我需要找到患有高血压和糖尿病的成员(ID)。这里的问题是DiagnosisCodes分布在10列。如何检查该成员是否符合这两个条件

DiagnosisCodes

+----+-------+-------+-------+-----+--------+
| ID | Diag1 | Diag2 | Diag3 | ... | Diag10 |
+----+-------+-------+-------+-----+--------+
| A  |  2502 |  2593 | NULL  | ... | NULL   |
| B  |  2F93 |  2509 | 2593  | ... | NULL   |
| C  |  C257 |  2509 | C6375 | ... | NULL   |
+----+-------+-------+-------+-----+--------+

DiagnosisConditions

+------+--------------+
| Code |  Condition   |
+------+--------------+
| 2502 | Hypertension |
| 2593 | Diabetes     |
| 2509 | Diabetes     |
| 2F93 | Hypertension |
| 2673 | HeartFailure |
+------+--------------+

预期结果

+---------+
| Members |
+---------+
| A       |
| B       |
+---------+

如何查询以检查多列中存在的多个值。你建议使用EXISTS吗?

SELECT DISTINCT id 
FROM   diagnosiscodes 
WHERE  ( diag1, diag2...diag10 ) IN (SELECT code 
                                     FROM   diagnosiscondition 
                                     WHERE  condition IN ( 'Hypertension','Diabetes' ) 
                                     ) 

2 个答案:

答案 0 :(得分:4)

我会使用group byhaving

执行此操作
select dc.id
from diagnosiscodes dc join
     diagnosiscondistions dcon
     on dcon.code in (dc.diag1, dc.diag2, . . . )
group by id
having sum(case when dcon.condition = 'diabetes' then 1 else 0 end) > 0 and
       sum(case when dcon.condition = 'Hypertension' then 1 else 0 end) > 0;

然后,您应该修复您的数据结构。具有由数字区分的相同信息的单独列通常是不良数据结构的标志。您应该有一个表格,称为PatientDiagnoses,如每个患者和诊断一行。

答案 1 :(得分:3)

这是通过取消数据的一种方式

SELECT DISTINCT id
FROM   yourtable
       CROSS apply (VALUES (Diag1),(Diag2),..(Diag10))tc(Diag)
WHERE  Diag IN (SELECT code
                FROM   diagnosiscondition
                WHERE  condition IN ( 'Hypertension', 'Diabetes' ) group by code having count(distinct condition)=2)