如何使用case语句或类似方法在where子句中定义逻辑

时间:2019-05-28 04:51:43

标签: sql

我需要基于一些预定义的逻辑定义天气以删除行,基本上是SAMPLE.SAMPLE_NAME =“ BLANK”,运行一个子查询,以检查值Result.NAME是否在子查询中,以及sample.SAMPLE_NAME <>“ BLANK”,然后什么也不做,不要检查只是保留所有值

SELECT DISTINCT SAMPLE.X_PROGRAM, SAMPLE.PROJECT, SAMPLE.SAMPLE_NUMBER, SAMPLE.RECD_DATE, SAMPLE.SAMPLE_NAME, SAMPLE.ORIGINAL_SAMPLE, TEST.ANALYSIS, RESULT.NAME, RESULT.FORMATTED_ENTRY,TEST.BATCH, 
RESULT.X_SHORT_NAME, RESULT.REPORTED_NAME, RESULT.X_LONG_NAME, RESULT.FACTOR_VALUE, RESULT.ENTRY_QUALIFIER AS QUALIFIER, RESULT.NUMERIC_ENTRY AS NRESULT, UNITS.DISPLAY_STRING, RESULT.X_QC_ANALYSIS_NAME, 
RESULT.X_QC_RESULT_NAME 
FROM (((SAMPLE INNER JOIN TEST ON SAMPLE.SAMPLE_NUMBER = TEST.SAMPLE_NUMBER) 
INNER JOIN RESULT ON test.test_NUMBER = RESULT.test_NUMBER) 
LEFT OUTER JOIN T_PROGRAM ON SAMPLE.X_PROGRAM = T_PROGRAM.NAME ) 
INNER JOIN UNITS ON RESULT.UNITS = UNITS.UNIT_CODE  
where SAMPLE.STANDARD = 'T'  AND SAMPLE.STATUS <> 'x' and TEST.STATUS <> 'x' and RESULT.STATUS <> 'x' and RESULT.REPORTABLE ='T'

AND TEST.BATCH IN (( SELECT BATCH.X_QC_LINK FROM (SAMPLE INNER JOIN TEST ON SAMPLE.SAMPLE_NUMBER = TEST.SAMPLE_NUMBER) INNER JOIN BATCH  ON TEST.BATCH = BATCH.NAME where BATCH.X_QC_LINK IS NOT NULL AND  SAMPLE.PROJECT='19-00002916')
union ( SELECT TEST.BATCH  FROM (SAMPLE INNER JOIN TEST ON SAMPLE.SAMPLE_NUMBER = TEST.SAMPLE_NUMBER) INNER JOIN RESULT ON TEST.TEST_NUMBER = RESULT.TEST_NUMBER where SAMPLE.PROJECT='19-00002916'))

and case when SAMPLE.SAMPLE_NAME ="BLANK" then and RESULT.NAME in (select R.name 
from Sample S, Result R , project P 
where p.name=s.project and S.sample_number=R.sample_number and S.standard='F' and R.status = 'A' and R.Reportable='T'  and S.Project='19-00002916' AND S.SAMPLE_NAME <>'CONTROL' )


AND SAMPLE.SAMPLE_NAME <> 'CONTROL'

0 个答案:

没有答案