对子查询的结果做一个案例

Do a case on the result of a subquery

我想知道如何在 SQL 中执行此操作。 从 result 我得到(RESULT 第二列 )。 我想检查这个结果是否在我的子查询第3列)的结果中,然后设置'Y'或'N' 在 SELECTED 中(第 4 列) 现在,我的 SQL 查询如下所示:

Select ANALYTE as Value, RESULT
,(select DISTINCT CHARLIMITS from SPEC_ANALYTES where SPEC_ANALYTES.ANALYTE = @ANALYTE AND SPEC_ANALYTES.TESTCODE = @TESTCODE ) as subquery,
case when subquery then 'Y'
     else 'N'
End as SELECTED
from POSSIBLERESULTS
where   ANALYTE = @ANALYTE AND TESTCODE = @TESTCODE

我有:

我想要什么:

我希望我说得通俗易懂! 感谢您花时间帮助我

蒂博。

您可以在 case 语句中使用 LIKE 和通配符匹配。为了不重复子查询,我将您当前的查询用作子查询,如下所示:

SELECT
    *,
    CASE
       WHEN subquery LIKE CONCAT('%',RESULT,'%') THEN 'Y'
       ELSE 'N'
    END as SELECTED
FROM (
    SELECT
        ANALYTE as Value, 
        RESULT,
        (select DISTINCT CHARLIMITS from SPEC_ANALYTES where SPEC_ANALYTES.ANALYTE = @ANALYTE AND SPEC_ANALYTES.TESTCODE = @TESTCODE ) as subquery,
   FROM 
       POSSIBLERESULTS
   WHERE 
       ANALYTE = @ANALYTE AND TESTCODE = @TESTCODE
) t