【发布时间】:2022-10-17 23:28:51
【问题描述】:
我必须编写一个查询来获取 100 多个诊断代码的行。什么是最佳 SQL 查询?以下是我所拥有的详细信息:
桌子:
| Diagnosis_Cd | Description |
|---|---|
| A00 | Cholera |
| A000 | Cholera due to Vibrio cholerae 01, biovar cholerae |
| A001 | Cholera due to Vibrio cholerae 01, biovar eltor |
| .... |
查找诊断代码列表只有前 3 个字符。这是在列表很小时编写查询的方式。
select Diagnosis_Cd
from <table>
where Diagnosis_Cd like 'A00%'
or Diagnosis_Cd like 'B00%'
or Diagnosis_Cd like 'N00%'
...
但现在该列表有 200 多个诊断代码可供查找。考虑到构建 WHERE 子句的容易程度,到目前为止,我可以提供以下内容。
WITH temp AS
(select substring(Diagnosis_Cd, 1, 3) as col1
from <table>)
select col1
from temp
where col1 in ('A00',
'B00',
'N00',
...)
有更好的方法吗? “正则表达式”似乎不能使用通配符。提前感谢您的任何指导。
【问题讨论】:
-
不确定它是否在每个 RDMBS 中,但至少其中一些使用
_作为单个字符——所以我认为like '_00%'会起作用 -
诊断代码在某些石斑鱼中不一定有共同的格式,因此可能会有
A00%与B10%相关的情况。您可能想从 AHRQ 下载 CCS 石斑鱼表。这有一些可能对您有帮助的标准组。
标签: sql substring where-clause wildcard regexp-substr