我正在研究Postgres,以下查询似乎永远不会完成执行,但它只是导致问题的最终OR,如果我将其注释掉它运行正常。只是它是一种低效的写作方式吗?如果是这样,我就无法想到另一种选择。
SELECT a.subject_id, a.hadm_id, a.admittime, d.icd9_code, l.short_title, d.seq_num
FROM mimiciii.admissions a
INNER JOIN mimiciii.diagnoses_icd d
ON a.subject_id = d.subject_id
AND a.hadm_id = d.hadm_id
INNER JOIN mimiciii.d_icd_diagnoses l
ON d.icd9_code = l.icd9_code
WHERE a.subject_id IN
(SELECT DISTINCT d.subject_id
FROM mimiciii.diagnoses_icd d
WHERE d.icd9_code BETWEEN '390%' and '459%')
AND d.icd9_code NOT IN
(SELECT d.icd9_code
FROM mimiciii.diagnoses_icd d
WHERE d.icd9_code BETWEEN 'V01%' AND 'V91%'
OR d.icd9_code BETWEEN 'E000%' AND 'E999%'
OR d.icd9_code BETWEEN '630%' AND '679%'
OR d.icd9_code BETWEEN '760%' AND '999%'
)
ORDER BY subject_id, admittime
答案 0 :(得分:2)
也许有一个原因它永远不会完成执行,因为BETWEEN
的过滤器所有都排除了。您不能将通配符与BETWEEN
一起使用。
整个子查询似乎没必要。你可以删除它。试试这个where
条款:
WHERE a.subject_id IN (SELECT d.subject_id
FROM mimiciii.diagnoses_icd d
WHERE d.icd9_code >= '390' and
d.icd9_code < '460'
)
如果代码与此匹配,那么它与您的其他条件不符。
我怀疑查询还有其他问题,但这个问题很明显。
答案 1 :(得分:0)
SELECT a.subject_id, a.hadm_id, a.admittime, d.icd9_code, l.short_title, d.seq_num
FROM mimiciii.admissions a
INNER JOIN mimiciii.diagnoses_icd d
ON a.subject_id = d.subject_id
AND a.hadm_id = d.hadm_id
INNER JOIN mimiciii.d_icd_diagnoses l
ON d.icd9_code = l.icd9_code
WHERE a.subject_id IN
(SELECT DISTINCT d.subject_id
FROM mimiciii.diagnoses_icd d
WHERE d.icd9_code BETWEEN '390%' and '459%')
AND d.icd9_code NOT IN
(SELECT d.icd9_code
FROM mimiciii.diagnoses_icd d
WHERE (d.icd9_code BETWEEN 'V01%' AND 'V91%')
OR (d.icd9_code BETWEEN 'E000%' AND 'E999%')
OR (d.icd9_code BETWEEN '630%' AND '679%')
OR (d.icd9_code BETWEEN '760%' AND '999%')
)
ORDER BY subject_id, admittime
您可以尝试(尚未测试)使用括号来命令子查询,在这种情况下,它将先评估括号,然后运行OR语句来收集数据以提供给外部查询。
答案 2 :(得分:0)
为什么需要一个子查询?
这不会回报你想要的东西吗?
SELECT a.subject_id, a.hadm_id, a.admittime, d.icd9_code, l.short_title, d.seq_num
FROM mimiciii.admissions a
INNER JOIN mimiciii.diagnoses_icd d
ON a.subject_id = d.subject_id
AND a.hadm_id = d.hadm_id
INNER JOIN mimiciii.d_icd_diagnoses l
ON d.icd9_code = l.icd9_code
WHERE d.icd9_code BETWEEN '390%' and '459%'
and not (d.icd9_code BETWEEN 'V01%' AND 'V91%')
and not (d.icd9_code BETWEEN 'E000%' AND 'E999%')
and not (d.icd9_code BETWEEN '630%' AND '679%')
and not (d.icd9_code BETWEEN '760%' AND '999%')
ORDER BY subject_id, admittime
或者我错了?