2017-12-28 48 views
0

아래의 첫 번째 사례 그룹 (Err1)은 첫 번째 When를 넘어 평가하지 않습니다. 다른 두 Case 그룹, Err2 및 Err3을 제거하지 않으면 예상대로 작동하지 않습니다. 원래 하나의 열을 생성하기 위해 이들을 연결하여 사용했지만 위와 같이 작동했습니다. 나는 이것을 분리 된 열로 분리하는 것이 내 문제를 해결할 것이라고 생각했지만 기쁨은 없었다.예상대로 작동하지 않는 경우 - Transact SQL

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' 

여러 가지 버전으로 시도했지만 다른 결과가 있지만 여전히 올바르지 않습니다. 어떤 멋진 아이디어라도 감사합니다.

감사

+1

글쎄, approval_date는 항상 'NULL'또는 '0'이어야합니다. –

+2

쿼리의 예상 및 실제 출력에 대한 샘플 데이터가 있습니까? –

+0

누락 된 elses를 중첩 된 사례에 추가해 보았습니까? – fabricio

답변

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' 
1

날짜가 날짜로 정의되지 않는 것 같습니다, 그리고, 당신이 알고있는 경우에 저를 용서하지만, 빈 문자열이 널 (null)과 동일하지 않습니다 .

아래 예제는 내가하고 싶은 생각을 보여 주지만 적절한 데이터 유형을 사용합니다. 적절한 데이터 유형을 사용하는 것이 좋습니다.

오해를 제거하기 위해 case 문에서 하나의 조건이 참하자마자 다음 "when"절이 평가되지 않습니다. err1, err2 및 err3은 독립적 인 case 문이므로 항상 모두 평가되어야하지만 중첩 된 case 문은 err1에 종속됩니다.

또한 char (13)은 캐리지 리턴입니다. 그러면 출력 라인을 덮어 씁니다. 아마도 당신은 개행 문자 인 char (10)을 원할 것입니다.

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

모두들 올바른 길을 가고 있습니다. 큰 문제는 Cases 중 일부가 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'