CASE无法按预期工作 - Transact SQL

问题描述 投票:0回答:3

下面的第一个案例组(Err1)不会超出第一个案例组,除非我删除其他两个案例组,Err2和Err3,然后它按预期工作。我最初将这些连接起来创建了一个列,但它的工作原理如上所述。我认为把它分成不同的列可以解决我的问题,但没有快乐。

SELECT 
    *, 
    CASE
       WHEN ISNULL(Approval_Date, 0) = 0 
          THEN 'Approval Date is Missing' + Char(13) + Char(13) 
          ELSE
             CASE WHEN First_Spend > (ISNULL(Approval_Date, 0) + 15)  
                     THEN 'Approval Date is too far in the past' + char(13) + Char(13) 
                  WHEN (First_Spend + 25) < ISNULL(Approval_Date, 0)  
                     THEN 'Approval Date is too far in the future' + char(13) + Char(13) 
             END
    END AS 'ERR1',
    CASE
       WHEN Funding_Status = '' 
          THEN 'Funding Status is Missing' + Char(13) + Char(13) 
    END AS 'ERR2',
    CASE
       WHEN Funding_Type = '' 
          THEN 'Funding Type is Missing' + Char(13) + Char(13) 
    END AS 'ERR3'

我已经尝试了几种不同的版本,但结果仍然不正确。任何好主意都赞赏。

谢谢

sql sql-server case
3个回答
1
投票

您的日期似乎没有被定义为日期,如果您知道这一点,请原谅我,但空字符串与null不同。

下面的示例显示了我认为您想要做的事情,但使用了适当的数据类型。我强烈建议使用适当的数据类型。

并且,为了消除任何误解,只要case语句中的一个条件为真,就不会评估以下“when”子句。 err1,err2和err3是独立的case语句,所以它们应该总是被评估,但嵌套的case语句依赖于err1。

此外,char(13)是回车。这将导致您的输出行被覆盖。也许您希望char(10)是换行符(或者如果您在Windows上运行则两者都是)。

create table so48010818
(
    id                int,
    approval_date     date,
    first_spend       date,
    funding_status    varchar(1),
    funding_type      varchar(1)
);

insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (1, null, '2017-12-28', '', '');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (2, '2017-12-20', '2017-12-28', '', '');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (3, '2017-12-12', '2017-12-28', '', '');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (4, '2017-12-27', '2017-12-01', '', '');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (5, null, '2017-12-28', null, null);
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (6, '2017-12-20', '2017-12-28', null, null);
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (7, '2017-12-12', '2017-12-28', null, null);
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (8, '2017-12-27', '2017-12-01', null, null);
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (5, null, '2017-12-28', 'X', 'Y');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (6, '2017-12-20', '2017-12-28', 'X', 'Y');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (7, '2017-12-12', '2017-12-28', 'X', 'Y');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (8, '2017-12-27', '2017-12-01', 'X', 'Y');

select * from so48010818;

select 
    id,
    approval_date,
    first_spend,
    funding_status,
    funding_type, 
    case 
        when approval_date is null then 'approval date is missing (null)'
    else               -- approval_date cannot be null here
        case
            when first_spend > dateadd(day, 15, approval_date) then 'approval date is too far in the past' 
            when dateadd(day, 25, first_spend) < approval_date then 'approval date is too far in the future'
        end
    end as 'err1',
    case 
        when funding_status is null then 'funding status is missing (null)' 
        when funding_status = '' then 'funding status is missing (empty string)' 
    end as 'err2',
    case 
        when funding_type is null then 'funding type is missing (null)' 
        when funding_type = '' then 'funding type is missing (empty string)'
    end as 'err3'
from
    so48010818;

id          approval_date    first_spend      funding_status funding_type err1                                  err2                                     err3
----------- ---------------- ---------------- -------------- ------------ ------------------------------------- ---------------------------------------- -----------------------------    --------
          1             NULL       2017-12-28                             approval date is missing (null)        funding status is missing (empty string) funding type is missing (empty string)
          2       2017-12-20       2017-12-28                             NULL                                   funding status is missing (empty string) funding type is missing (empty string)
          3       2017-12-12       2017-12-28                             approval date is too far in the past   funding status is missing (empty string) funding type is missing (empty string)
          4       2017-12-27       2017-12-01                             approval date is too far in the future funding status is missing (empty string) funding type is missing (empty string)
          5             NULL       2017-12-28 NULL           NULL         approval date is missing (null)        funding status is missing (null)         funding type is missing (null)
          6       2017-12-20       2017-12-28 NULL           NULL         NULL                                   funding status is missing (null)         funding type is missing (null)
          7       2017-12-12       2017-12-28 NULL           NULL         approval date is too far in the past   funding status is missing (null)         funding type is missing (null)
          8       2017-12-27       2017-12-01 NULL           NULL         approval date is too far in the future funding status is missing (null)         funding type is missing (null)
          5             NULL       2017-12-28 X              Y            approval date is missing (null)        NULL                                     NULL
          6       2017-12-20       2017-12-28 X              Y            NULL                                   NULL                                     NULL
          7       2017-12-12       2017-12-28 X              Y            approval date is too far in the past   NULL                                     NULL
          8       2017-12-27       2017-12-01 X              Y            approval date is too far in the future NULL                                     NULL

(12 rows affected)

0
投票

试试这个:

SELECT  *, 
        CASE
            WHEN ISNULL(Approval_Date, 0) = 0 THEN 'Approval Date is Missing' + Char(13) + Char(13) 

            WHEN First_Spend > (ISNULL(Approval_Date, 0) + 15) THEN 'Approval Date is too far in the past' + char(13) + Char(13) 

            WHEN (First_Spend + 25) < ISNULL(Approval_Date, 0) THEN 'Approval Date is too far in the future' + char(13) + Char(13) 
        END AS 'ERR1',
        CASE
            WHEN Funding_Status = '' THEN 'Funding Status is Missing' + Char(13) + Char(13) 
        END AS 'ERR2',
        CASE
            WHEN Funding_Type = '' THEN 'Funding Type is Missing' + Char(13) + Char(13) 
        END AS 'ERR3'

0
投票

谢谢你们,你们都在正确的轨道上。最大的问题是一些案件返回null。原始版本实际上有效,但它返回了“批准日期缺失”和“批准日期过去太远”的错误,这可能是同样的错误。

我做了一些更改,我添加了Elses,我使用我的isnull值进行审批日期以消除重复错误,并修复了丢失First_Spend日期的可能性。它现在看起来像这样:

SELECT *, 
Case When Approval_Date is null then 'Approval Date is Missing' + Char(13) + Char(13) Else ''End +
Case When Funding_Status = '' then 'Funding Status is Missing' + Char(13) + Char(13) Else '' End +
Case When Funding_Type = '' then 'Funding Type is Missing' + Char(13) + Char(13) Else '' End +

Case 
    When ISNULL(First_Spend,0) >(isnull(Approval_Date,CAST('12/12/2099' AS DATETIME)) + 15)  then 'Approval Date is too far in the past' + char(13) + Char(13) 
    When (ISNULL(First_Spend,CAST('12/12/2099' AS DATETIME)) +25) < isnull(Approval_Date,0)  then 'Approval Date is too far in the future' + char(13) + Char(13)
    Else '' End as 'ERR'
© www.soinside.com 2019 - 2024. All rights reserved.