我陷入了多重选择

时间:2014-03-25 09:03:52

标签: sql sql-server-2008

我坚持这个因为我有像这样的多重选择

SELECT * FROM
  (
 SELECT CASE 
 WHEN co.grdtxtCertificationOther LIKE '%อย.%' THEN  2
 WHEN co.grdtxtCertificationOther LIKE '%มผช%' THEN 3 
 WHEN co.grdtxtCertificationOther LIKE '%มอก.%' THEN 4 
 WHEN co.grdtxtCertificationOther LIKE '%ฮาลาล%' THEN 5
 WHEN co.grdtxtCertificationOther LIKE '%Q%' THEN 6 
 WHEN co.grdtxtCertificationOther LIKE '%GMP%' THEN 7 
 WHEN co.grdtxtCertificationOther LIKE '%GAP%' THEN 8 
 WHEN co.grdtxtCertificationOther LIKE '%HACCP%' THEN 9 
 WHEN co.grdtxtCertificationOther LIKE '%เกษตรอินทรีย์%' THEN 10 
 ELSE 1 END As ID
        , co.[grdtxtCertificationOther] AS STDName
        , co.[grdtxtCertificationNumberOther] AS STDNumber
        , CONVERT(VARCHAR(24),
           CASE 
               WHEN LEN(co.grddatIssueDateOther) >4 
               THEN co.grddatIssueDateOther
           END,109) As SentDate  
        , dp.libtxtUserID  As ParentID
        , 1 AS Displayorder
        , 0 AS isDisable
FROM Custom.tblR_docProduct dp
INNER JOIN  [Custom].[tblR_docProduct_grdCertificationOther] co
ON dp.Id = co._Parent) t
WHERE NOT t.SentDate IS NULL
AND NOT t.STDName IS NULL
AND NOT t.STDNumber IS NULL

我需要它们来更改STDName中包含的值,其ID是2-9到NULL

但在我将它们命名为t之前我无法使用表达式 如何添加更多if子句或案例?

我需要添加一个像这样的表达式

if t.id <> 1 then t.STDName = NULL

3 个答案:

答案 0 :(得分:0)

也许这可以帮助您构建查询。

SELECT star
     , is
     , evil
     , CASE WHEN evil = 1 THEN star ELSE NULL END As we_can_refer_to_this_now
FROM   (
        SELECT star
             , is
             , CASE WHEN column = 'a' THEN 1 ELSE 0 END As evil
        FROM   ...
       ) As a_subquery
WHERE  evil IN (1, 0)

一旦你将内部查询作为一个子查询(用括号括起来并给它一个别名),外部查询就可以访问其中的列。

答案 1 :(得分:0)

您只需要再次重复整个CASE,这次选择STDName

SELECT * FROM
  (
 SELECT CASE 
 WHEN co.grdtxtCertificationOther LIKE '%อย.%' THEN  2
 WHEN co.grdtxtCertificationOther LIKE '%มผช%' THEN 3 
 WHEN co.grdtxtCertificationOther LIKE '%มอก.%' THEN 4 
 WHEN co.grdtxtCertificationOther LIKE '%ฮาลาล%' THEN 5
 WHEN co.grdtxtCertificationOther LIKE '%Q%' THEN 6 
 WHEN co.grdtxtCertificationOther LIKE '%GMP%' THEN 7 
 WHEN co.grdtxtCertificationOther LIKE '%GAP%' THEN 8 
 WHEN co.grdtxtCertificationOther LIKE '%HACCP%' THEN 9 
 WHEN co.grdtxtCertificationOther LIKE '%เกษตรอินทรีย์%' THEN 10 
 ELSE 1 END As ID,
 CASE 
     WHEN co.grdtxtCertificationOther LIKE '%อย.%' THEN NULL
     WHEN co.grdtxtCertificationOther LIKE '%มผช%' THEN NULL 
     WHEN co.grdtxtCertificationOther LIKE '%มอก.%' THEN NULL
     WHEN co.grdtxtCertificationOther LIKE '%ฮาลาล%' THEN NULL
     WHEN co.grdtxtCertificationOther LIKE '%Q%' THEN NULL 
     WHEN co.grdtxtCertificationOther LIKE '%GMP%' THEN NULL 
     WHEN co.grdtxtCertificationOther LIKE '%GAP%' THEN NULL
     WHEN co.grdtxtCertificationOther LIKE '%HACCP%' THEN NULL
             WHEN co.grdtxtCertificationOther LIKE '%เกษตรอินทรีย์%' THEN NULL 
     ELSE co.[grdtxtCertificationOther] 
END AS STDName
        , co.[grdtxtCertificationNumberOther] AS STDNumber
        , CONVERT(VARCHAR(24),
           CASE 
               WHEN LEN(co.grddatIssueDateOther) >4 
               THEN co.grddatIssueDateOther
           END,109) As SentDate  
        , dp.libtxtUserID  As ParentID
        , 1 AS Displayorder
        , 0 AS isDisable
FROM Custom.tblR_docProduct dp
INNER JOIN  [Custom].[tblR_docProduct_grdCertificationOther] co
ON dp.Id = co._Parent) t
WHERE NOT t.SentDate IS NULL
AND NOT t.STDName IS NULL
AND NOT t.STDNumber IS NULL

答案 2 :(得分:0)

您可以将以下内容添加到初始查询中:

SELECT
 ID,
 CASE 
  WHEN ID = 1 THEN STDName ELSE NULL END AS STDName,
 STDNumber,
 SentDate,
 ParentID,
 Displayorder,
 isDisable
FROM (_YOUR_INITIAL_QUERY_) Q1