select distinct a.EMPI_ID, a.PATIENT_NO, a.MR_NO, a.PAT_NAME, a.PAT_SEX, a.PAT_AGE, a.PAT_PHONE_NO, b.DIAG_RESULT, a.ADMIT_DATE, a.DISCHARGE_DEPT_NAME, a.ATTEND_DR from BASIC_INFORMATION a join PA_DIAG b on a.MZZY_SERIES_NO=b.MZZY_SERIES_NO join EXAM_DESC_RESULT_CODE c on a.MZZY_SERIES_NO=c.MZZY_SERIES_NO join DRUG_INFO d on a.MZZY_SERIES_NO=d.MZZY_SERIES_NO join EMR_CONTENT e on a.MZZY_SERIES_NO=e.MZZY_SERIES_NO JOIN TEST_INFO A17 ON a.MZZY_SERIES_NO = A17.MZZY_SERIES_NO where a.PAT_AGE>='18' and (to_char(a.ADMIT_DATE,'YYYY-MM-DD') >= '2021-01-01') AND (b.DIAG_RESULT LIKE '%鼻咽癌%' or b.DIAG_RESULT LIKE '%鼻咽恶性肿瘤%' or b.DIAG_CODE LIKE '%C11/900%') and d.DRUG_NAME not in (select DRUG_NAME FROM DRUG_INFO WHERE DRUG_NAME like '卡培他滨') and b.DIAG_RESULT NOT IN (SELECT DIAG_RESULT FROM PA_DIAG WHERE DIAG_RESULT LIKE '%HIV阳性%') and b.DIAG_RESULT NOT IN (SELECT DIAG_RESULT FROM PA_DIAG WHERE DIAG_RESULT LIKE '%充血性心力衰竭%') AND to_char(( A17.TEST_DETAIL_ITEM_NAME = '中性粒细胞' AND A17.TEST_RESULT >= 1.5 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '血小板' AND A17.TEST_RESULT >= 100 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '血红蛋白' AND A17.TEST_RESULT >= 9 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '丙氨酸氨基转移酶' AND A17.TEST_RESULT <= 2.5 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '天门冬氨酸氨基转移酶' AND A17.TEST_RESULT <= 2.5 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '肌酐清除率' AND A17.TEST_RESULT > 51 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '肌酐' AND A17.TEST_RESULT <=1.5 ) OR ( A17.TEST_DETAIL_ITEM_NAME = '凝血酶原时间' AND A17.TEST_RESULT <= 1.5 ))语句哪里有问题
时间: 2023-10-09 16:07:38 浏览: 131
select distinct用法
这个 SQL 语句中有一个问题,就是在最后一个 AND 子句中的 to_char() 函数的参数不正确。to_char() 函数需要一个日期或数字类型的参数,但是这里传入的是一个逻辑类型的表达式。
如果你想要在 WHERE 子句中使用逻辑表达式,则可以使用 CASE WHEN 语句来实现。下面是修改后的 SQL 语句:
```
SELECT DISTINCT
a.EMPI_ID,
a.PATIENT_NO,
a.MR_NO,
a.PAT_NAME,
a.PAT_SEX,
a.PAT_AGE,
a.PAT_PHONE_NO,
b.DIAG_RESULT,
a.ADMIT_DATE,
a.DISCHARGE_DEPT_NAME,
a.ATTEND_DR
FROM
BASIC_INFORMATION a
JOIN PA_DIAG b ON a.MZZY_SERIES_NO = b.MZZY_SERIES_NO
JOIN EXAM_DESC_RESULT_CODE c ON a.MZZY_SERIES_NO = c.MZZY_SERIES_NO
JOIN DRUG_INFO d ON a.MZZY_SERIES_NO = d.MZZY_SERIES_NO
JOIN EMR_CONTENT e ON a.MZZY_SERIES_NO = e.MZZY_SERIES_NO
JOIN TEST_INFO A17 ON a.MZZY_SERIES_NO = A17.MZZY_SERIES_NO
WHERE
a.PAT_AGE >= '18'
AND (TO_CHAR(a.ADMIT_DATE, 'YYYY-MM-DD') >= '2021-01-01')
AND (
b.DIAG_RESULT LIKE '%鼻咽癌%'
OR b.DIAG_RESULT LIKE '%鼻咽恶性肿瘤%'
OR b.DIAG_CODE LIKE '%C11/900%'
)
AND d.DRUG_NAME NOT IN (
SELECT DRUG_NAME
FROM DRUG_INFO
WHERE DRUG_NAME LIKE '卡培他滨'
)
AND b.DIAG_RESULT NOT IN (
SELECT DIAG_RESULT
FROM PA_DIAG
WHERE DIAG_RESULT LIKE '%HIV阳性%'
)
AND b.DIAG_RESULT NOT IN (
SELECT DIAG_RESULT
FROM PA_DIAG
WHERE DIAG_RESULT LIKE '%充血性心力衰竭%'
)
AND (
CASE
WHEN A17.TEST_DETAIL_ITEM_NAME = '中性粒细胞' AND A17.TEST_RESULT >= 1.5 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '血小板' AND A17.TEST_RESULT >= 100 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '血红蛋白' AND A17.TEST_RESULT >= 9 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '丙氨酸氨基转移酶' AND A17.TEST_RESULT <= 2.5 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '天门冬氨酸氨基转移酶' AND A17.TEST_RESULT <= 2.5 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '肌酐清除率' AND A17.TEST_RESULT > 51 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '肌酐' AND A17.TEST_RESULT <= 1.5 THEN 'Y'
WHEN A17.TEST_DETAIL_ITEM_NAME = '凝血酶原时间' AND A17.TEST_RESULT <= 1.5 THEN 'Y'
ELSE 'N'
END) = 'Y';
```
在最后一个 AND 子句中,使用了 CASE WHEN 语句来判断测试结果是否符合条件。如果测试结果符合条件,则返回 Y,否则返回 N。然后再判断返回值是否等于 Y。
阅读全文