具有多个子条件的嵌套案例

Nested case with multiple sub conditions

我无法理解如何正确嵌套 case 语句。 (MSSQL 服务器 2012)

让我们给出以下 table。 列 StatusMissing 是我想要创建的

+------+--+------+--+------+--+------+--+------+--+------+--+---------------+
|  a1  |  |  a2  |  |  a3  |  |  b1  |  |  c1  |  |  d2  |  | StatusMissing |
+------+--+------+--+------+--+------+--+------+--+------+--+---------------+
| OK   |  | OK   |  | OK   |  | OK   |  | OK   |  | OK   |  | AllOK         |
| NULL |  | NULL |  | OK   |  | OK   |  | OK   |  | OK   |  | As            |
| OK   |  | NULL |  | OK   |  | OK   |  | OK   |  | OK   |  | As            |
| OK   |  | OK   |  | NULL |  | OK   |  | OK   |  | OK   |  | As            |
| OK   |  | OK   |  | OK   |  | NULL |  | OK   |  | OK   |  | B             |
| OK   |  | OK   |  | OK   |  | OK   |  | NULL |  | OK   |  | C             |
| OK   |  | OK   |  | OK   |  | OK   |  | OK   |  | NULL |  | D             |
| NULL |  | OK   |  | OK   |  | NULL |  | NULL |  | OK   |  | ABC           |
| NULL |  | OK   |  | OK   |  | OK   |  | NULL |  | NULL |  | ACD           |
| NULL |  | OK   |  | OK   |  | NULL |  | OK   |  | NULL |  | ABD           |
| NULL |  | OK   |  | OK   |  | NULL |  | NULL |  | NULL |  | ABCD          |
| NULL |  | OK   |  | OK   |  | OK   |  | NULL |  | NULL |  | ACD           |
| OK   |  | OK   |  | OK   |  | NULL |  | NULL |  | OK   |  | BC            |
| OK   |  | OK   |  | OK   |  | OK   |  | OK   |  | OK   |  | AllOK         |
| OK   |  | NULL |  | OK   |  | OK   |  | NULL |  | OK   |  | AC            |
| OK   |  | OK   |  | OK   |  | NULL |  | OK   |  | NULL |  | BD            |
| OK   |  | OK   |  | OK   |  | OK   |  | NULL |  | NULL |  | CD            |
+------+--+------+--+------+--+------+--+------+--+------+--+---------------+

首先,为了理解嵌套的概念,我简化了 table:

+------+--+------+--+------+
|  a1  |  |  a2  |  |  b1  |
+------+--+------+--+------+
| OK   |  | OK   |  | OK   |
| OK   |  | OK   |  | NULL |
| OK   |  | NULL |  | OK   |
| NULL |  | OK   |  | OK   |
| NULL |  | NULL |  | OK   |
| NULL |  | OK   |  | NULL |
| OK   |  | NULL |  | NULL |
+------+--+------+--+------+

这些尝试导致了这些失败。 查询 1

SELECT a1, a2, b1 'StatusMissing' =
CASE 
        WHEN a1 IS NULL
        THEN 
                CASE
                        WHEN a1 IS NULL
                        THEN 
                                CASE
                                        WHEN b1 IS NULL
                                        THEN 'AB'
                                END
                        ELSE 'A'
                END

        WHEN b1 IS NULL
        THEN 'B'
        ELSE 'AllOK'
END
FROM Table;

结果 1:

+------+--+------+--+------+--+---------------+
|  a1  |  |  a2  |  |  b1  |  | StatusMissing |
+------+--+------+--+------+--+---------------+
| OK   |  | OK   |  | OK   |  | AllOK         |
| OK   |  | OK   |  | NULL |  | B             |
| OK   |  | NULL |  | OK   |  | AllOK         |
| NULL |  | OK   |  | OK   |  | NULL          |
| NULL |  | NULL |  | OK   |  | NULL          |
| NULL |  | OK   |  | NULL |  | AB            |
| OK   |  | NULL |  | NULL |  | B             |
+------+--+------+--+------+--+---------------+

Query2(其他作为主要)

SELECT a1, a2, b1, 'Status' =
CASE 
        WHEN a1 IS NOT NULL AND a2 IS NOT NULL AND b1 IS NOT NULL
        THEN 'AllOK!'
        ELSE
                CASE
                        WHEN a2 IS NOT NULL OR a2 IS NOT NULL
                        THEN 
                                CASE
                                        WHEN b1 IS NULL
                                        THEN 'AB'
                                END

                        WHEN b1 IS NULL
                        THEN 'B'

                        ELSE 'A'
                END
END
FROM Table;

结果2

+------+--+------+--+------+--+---------------+
|  a1  |  |  a2  |  |  b1  |  | StatusMissing |
+------+--+------+--+------+--+---------------+
| OK   |  | OK   |  | OK   |  | AllOK         |
| OK   |  | OK   |  | NULL |  | AB            |
| OK   |  | NULL |  | OK   |  | A             |
| NULL |  | OK   |  | OK   |  | NULL          |
| NULL |  | NULL |  | OK   |  | A             |
| NULL |  | OK   |  | NULL |  | AB            |
| OK   |  | NULL |  | NULL |  | B             |
+------+--+------+--+------+--+---------------+

我到底做错了什么? 我对 SQL 很陌生,所以如果有合适的功能可以做到这一点,我将不胜感激! 编辑: 如果这样的事情在 SQL 中是可能的,我的意思是: 列 StatusMissing = '缺失' 如果(a1 == NULL){ StatusMissing += 'A'}

编辑 2:

StatusMissing 列不存在! 我想使用下面的 SQL 语句创建它。 SELECT ....状态=

所以基本上我只有 A1、A2、B1(在简单的 table 中)。请不要与第一个 Table 混淆。它只是用来展示它应该是什么样子的。

对于简化的table,假设数据类型为nvarchar

尝试使用 UPDATE-

  UPDATE [dbo].[StatusMissing]
  SET result='';

  UPDATE [dbo].[StatusMissing]
  SET result= CONCAT(result , 'A')
  WHERE a1 is null or a2 is null;


  UPDATE [dbo].[StatusMissing]
  SET result= CONCAT(result , 'B')
  WHERE b1 is null ;

  UPDATE [dbo].[StatusMissing]
  SET result= 'AllOK'
  WHERE result ='';

这也可以一步完成。

您可以玩 COALESCE 和几个 CASE 条件

SELECT a1,
  a2,
  a3,
  b1,
  c1,
  d2,
  COALESCE(
    CASE WHEN 
      b1 = 'OK' 
      AND c1 = 'OK' 
      AND d2 = 'OK' 
      AND (a1 IS NULL OR a2 IS NULL OR a3 is NULL) 
    THEN 'As' 
    ELSE ''
    END,
    CASE WHEN 
      (a1 IS NULL OR a2 IS NULL or a3 is NULL) 
    THEN 'A' 
    END  
    + CASE WHEN 
        b1 IS NULL 
      THEN 'B'
      ELSE '' 
      END
    + CASE WHEN 
        c1 IS NULL 
      THEN 'C'
      ELSE '' 
      END 
    + CASE WHEN 
        d2 IS NULL 
      THEN 'D' 
      ELSE ''
    END,
    'AllOK') AS 'StatusMissing'
FROM Table;

我可能会建议您对输出进行两个小修改:

  • 而不是 "As",只需说 "A"。
  • 而不是 "AllOK",只需将该字段留空即可。

经过这些修改,规则变得非常简单:

select t.*,
       ((case when a1 is null or a2 is null or a3 is null then 'A' else '' end) +
        (case when b1 is null then 'B' else '' end) +
        (case when c1 is null then 'C' else '' end) +
        (case when d1 is null then 'D' else '' end)
       ) as StatusMissing
from table t;

如果您确实需要自己的版本,子查询可能是最简单的方法:

select t. . . .,
       (case when StatusMissing = '' then 'AllOK'
             when StatusMissing = 'A' then 'As'
             else StatusMissing
        end) as StatusMissing
from (select t.*,
             ((case when a1 is null or a2 is null or a3 is null then 'A' else '' end) +
              (case when b1 is null then 'B' else '' end) +
              (case when c1 is null then 'C' else '' end) +
              (case when d1 is null then 'D' else '' end)
             ) as StatusMissing
      from table t
     ) t