2

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

我的目标是,如果 DueDate 为 NULL,因为 Table2 中没有记录,因此我想要的只是将此值显示为“否”而不是“NULL”。我在代码中添加了“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

需要帮助请叫我。

类似/相关问题:

4

5 回答 5

5

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

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 ;
于 2012-12-06T13:32:58.533 回答
2

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

于 2012-12-06T13:48:53.153 回答
0

这项工作是否可行 - 怀疑其中可能存在一些故障,但我目前没有管理 sudio:

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
于 2012-12-06T13:27:16.243 回答
0

我认为问题是如果DueDateNULL 你的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'    
于 2012-12-06T13:23:15.927 回答
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
于 2012-12-06T12:24:02.463 回答