SQL - CASE语句如果记录为NULL,因为表中不存在记录

时间:2012-12-06 12:04:15

标签: sql sql-server sql-server-2008-r2

我有一个问题,我认为其中一个问题会在底部解决,但我仍然有问题。问题是我的案例陈述是错误的(但运行没有错误)但不确定我还缺少什么。

我的目标是,如果DueDate为NULL,因为Table2中没有记录,因此我想要的是将此值显示为' No'而不是' NULL'。我添加了#34; IsNull"进入我的代码,但还没有解决我的问题。我在这些论坛上看过ISNULL()和COALEASE(),但无法得到我想要的结果

以下是我的工作代码:

SELECT 
    Client, 
    COUNT(*) AS ReadyRecords, 
    (SELECT COUNT(*) FROM Table1 EPR WHERE actioned=8 AND EPR.Client=Table1.Client) AS Outstanding, 
    (SELECT TOP 1 
        CASE 
            WHEN DueDate < GETDATE() THEN 'Yes' 
            WHEN DueDate IS NULL THEN 'No' 
        ELSE 'No' 
        END AS DueDate
    FROM Table2 GL WHERE GL.Client=Table1.Client ORDER BY DueDate) AS DueDate

    FROM Table1 WHERE Actioned=2 GROUP BY Client

编辑:

当前结果集:

Client   | ReadyRecords  |  Outstanding  |  DueDate

Test     |     42        |      54       |   NULL
Stack    |     150       |      10       |   NULL
JBloggs  |     32        |      28       |   Yes
DBloggs  |     2         |      17       |   Yes
Overflow |     1         |      1        |   No

期望的结果集:

Client   | ReadyRecords  |  Outstanding  |  DueDate

Test     |     42        |      54       |   No
Stack    |     150       |      10       |   No
JBloggs  |     32        |      28       |   Yes
DBloggs  |     2         |      17       |   Yes
Overflow |     1         |      1        |   No

表1:

id      |  Client   |   Actioned  | etc...

5245    |  Test     |      8      | etc...
....so on

表2:

id   |   Client   |   DueDate

1    |  JBloggs   | 2012-12-01 00:00:00.000
2    |  DBloggs   | 2012-12-05 00:00:00.000
3    |  Overflow  | 2012-12-12 00:00:00.000

如果您还有其他需要,请告诉我。

相似/相关问题:

5 个答案:

答案 0 :(得分:5)

您需要将ISNULL()COALESCE()函数应用于整个内联子查询,如下所示:

SELECT 
    Client, 
    COUNT(*) AS ReadyRecords, 
    (SELECT COUNT(*) FROM Table1 EPR 
     WHERE actioned=8 AND EPR.Client=Table1.Client
    ) AS Outstanding, 

    COALESCE(
        (SELECT TOP 1 
            CASE 
                WHEN DueDate < GETDATE() THEN 'Yes' 
            END AS DueDate
        FROM Table2 GL WHERE GL.Client=Table1.Client ORDER BY DueDate
        ), 'No' 
    ) AS DueDate

FROM Table1  WHERE Actioned=2  GROUP BY Client ;

或将CASE表达式移到那里:

SELECT 
    Client, 
    COUNT(*) AS ReadyRecords, 
    (SELECT COUNT(*) FROM Table1 EPR 
     WHERE actioned=8 AND EPR.Client=Table1.Client
    ) AS Outstanding, 

    CASE WHEN 
           (SELECT TOP 1 
              DueDate
            FROM Table2 GL WHERE GL.Client=Table1.Client ORDER BY DueDate
           ) < GETDATE() 
        THEN 'Yes'
        ELSE 'No'
    END AS DueDate

FROM Table1  WHERE Actioned=2  GROUP BY Client ;

答案 1 :(得分:2)

首先,您需要知道,表中的datetime字段可能显示为NULL,但在查询中使用它时,sql会将其转换为该数据类型允许的最小日期,即'1900-01 -01'所以你可能得到比实际值更多的'是'。

答案 2 :(得分:0)

You can use this...
SELECT 
Client, 
COUNT(*) AS ReadyRecords, 
(SELECT COUNT(*) FROM Table1 EPR WHERE actioned=8 AND EPR.Client=Table1.Client) AS Outstanding, 
(SELECT TOP 1 
    CASE 
        WHEN DueDate < GETDATE() THEN 'Yes' 
        ELSE ISNULL(Cast(DueDate AS VARCHAR),'NO' )        
    END AS DueDate
FROM Table2 GL WHERE GL.Client=Table1.Client ORDER BY DueDate) AS DueDate

FROM Table1 WHERE Actioned=2 GROUP BY Client

答案 3 :(得分:0)

我认为问题是如果DueDate为NULL,则DueDate < GetDate()结果为NULL,结果为No,因为DueDate在这种情况下永远不会低于GetDate。像这样更改订单

CASE 
    WHEN DueDate IS NULL THEN 'No'
    WHEN DueDate < GETDATE() THEN 'Yes'        
ELSE 'No' 
END AS DueDate

通过这样做,在其他条件之前首先检查NULL值。或者,您可以将此行更改为

WHEN ISNULL(DueDate,'Some Valid Default Value Here') < GETDATE() THEN 'Yes'    

答案 4 :(得分:0)

这是否有效 - 怀疑其中可能存在一些故障,但我目前没有管理层:

SELECT 
        Client, 
        COUNT(*) AS ReadyRecords, 
        (SELECT COUNT(*) FROM Table1 EPR WHERE actioned=8 AND EPR.Client=Table1.Client) AS Outstanding, 
        [DueDate] = CASE 
                                WHEN [MXDATE] < GETDATE() THEN 'Yes' 
                                WHEN [MXDATE] IS NULL THEN 'No' 
                            ELSE 'No' 
                            END 
FROM 
    Table1 T1
    LEFT OUTER JOIN
        (
        SELECT 
            Client,
            [MXDATE] = MAX(DueDate)
        FROM Table2
            GROUP BY Client
        ) GL
        ON
         GL.Client=T1.Client 
WHERE Actioned=2 
GROUP BY Client