我想在sql

时间:2017-04-13 11:29:38

标签: sql

我想在输出中添加默认列:

awbno     booking_date    Inscan_date    DR_date          
-----------------------------------------------------
111       10/03/2017      10/03/2017    15/02/2017  
222        is null        15/03/2017     is null       
333       05/04/2017       is null      17/02/2017  
444       15/02/2017       15/02/2017    18/02/2017 
555        is null         16/02/2017    19/02/2017    
666      28/02/2017        17/02/2017     20/02/2017    
777      01/03/2017        18/02/2017      is null      

我想这样输出:

awbno     booking_date     Inscan_date    DR_date        default
-----------------------------------------------------------------
111       10/03/2017       10/03/2017    15/02/2017 
222        is null         15/03/2017    is null       booking_Inscan_fail
333       05/04/2017       is null       17/02/2017      Inscan_fail
444       15/02/2017       15/02/2017    18/02/2017 
555        is null         16/02/2017    19/02/2017     booking_fail
666       28/02/2017       17/02/2017    20/02/2017 
777       01/03/2017       18/02/2017    is null        Dr_fail

3 个答案:

答案 0 :(得分:2)

我试试这样:

 SELECT awbno
 , booking_date
 , Inscan_date
 , DR_date
 , NULLIF(CASE WHEN booking_date IS NULL THEN 'booking_' ELSE '' END 
        + CASE WHEN Inscan_date IS NULL THEN 'Inscan_' ELSE '' END
        + CASE WHEN DR_date IS NULL THEN 'DR_' ELSE '' END,'') + 'fail' AS 'Result'
FROM SomeTable

查询将连接一个字符串,其中所有NULL值都会导致一个条目。

如果每列都有一个值,结果将是一个空字符串。在这种情况下,NULLIF会返回NULL,因此+ 'fail'也会返回NULL

答案 1 :(得分:1)

根据我的理解,你可能需要这样的事情:

SELECT awbno
 , booking_date
 , Inscan_date
 , DR_date
 , CONCAT(
    (CASE WHEN booking_date IS NULL THEN 'booking_' ELSE ''), 
    (CASE WHEN WHEN Inscan_date IS NULL THEN 'Inscan_' ELSE ''), 
    (CASE WHEN DR_date IS NULL THEN 'Dr_' ELSE ''),
    (CASE WHEN Inscan_date IS NULL OR
               booking_date IS NULL OR
               DR_date IS NULL THEN 'fail' ELSE '')
 ) as default
FROM <yourtable>

当一个或多个列为空时,会添加'booking_''Inscan_''Dr_'

答案 2 :(得分:0)

试一试:给定值需要多种组合

SELECT CASE 
               WHEN booking_date IS NULL AND Inscan_date IS NULL AND DR_date IS NULL 
               THEN 'booking_Inscan_fail'
               WHEN booking_date IS NULL AND Inscan_date IS NOT NULL AND DR_date IS NOT NULL
               THEN THEN 'booking_fail'
               WHEN booking_date IS NULL AND Inscan_date IS NULL AND DR_date IS NOT NULL
               THEN THEN 'booking_Inscan_fail' 
               WHEN booking_date IS NULL AND Inscan_date IS NOT NULL AND DR_date IS NULL
               THEN THEN 'booking_DR_fail' 
               WHEN booking_date IS NOT NULL AND Inscan_date IS NULL AND DR_date IS NOT NULL
               THEN THEN 'Inscan_fail'    
               WHEN booking_date IS NOT NULL AND Inscan_date IS NULL AND DR_date IS NULL
               THEN THEN 'Inscan_DR_fail' 
               WHEN booking_date IS NOT NULL AND Inscan_date IS NOT NULL AND DR_date IS NULL
               THEN THEN 'DR_fail' 
        END