在以下情况下使用两列:

时间:2018-08-28 08:33:51

标签: sql sql-server

我正在使用Case When基于两列计算五列。但这并没有给出正确的价值。我已附上正确结果的屏幕截图。 结果需要像: 业务逻辑是-这五个计算字段具有不同的定义,并取决于先前的字段。 EER的定义是,如果某个ID从某个队列传递而又未通过FBR,CVL和CSP等队列。在一个ID的所有行中,EER字段均应为Yes。其余四个计算字段的处理方式与when语句相同。一旦计算出EER,就应为其余ID计算EES。所以我用EER来表示EES需要为空。一个ID在五个字段之一中为“是”,对于其余四个ID则为空。

CREATE TABLE Table1
    ([ID] int, [QUEUE] varchar(7), [EER] varchar(4), [EES] varchar(4), [EEN] varchar(4), [EBNP] varchar(4), [NSB] varchar(4))
;

INSERT INTO Table1
    ([ID], [QUEUE], [EER], [EES], [EEN], [EBNP], [NSB])
VALUES
    (24017879, 'LOCCLOS', NULL, NULL, NULL, NULL, 'Yes'),
    (24017879, 'OTBCUST', NULL, NULL, NULL, NULL, 'Yes'),
    (24017879, 'CVLPLAN', NULL, NULL, NULL, NULL, 'Yes'),
    (24017879, 'LOCSCHD', NULL, NULL, NULL, NULL, 'Yes'),
    (24017879, 'LOCINST', NULL, NULL, NULL, NULL, 'Yes'),
    (24017879, 'BDWXNG', NULL, NULL, NULL, NULL, 'Yes'),
    (24017879, 'RESOLVE', NULL, NULL, NULL, NULL, 'Yes'),
    (24017879, 'BDWPLAN', NULL, NULL, NULL, NULL, 'Yes'),
    (24048916, 'PORSCHD', NULL, 'Yes', NULL, NULL, NULL),
    (24048916, 'VCECNFG', NULL, 'Yes', NULL, NULL, NULL),
    (24048916, 'VCEROUT', NULL, 'Yes', NULL, NULL, NULL),
    (24048916, 'LOCCLOS', NULL, 'Yes', NULL, NULL, NULL),
    (24048916, 'OTBCUST', NULL, 'Yes', NULL, NULL, NULL),
    (24019969, 'LOCCLOS', NULL, NULL, NULL, 'Yes', NULL),
    (24019969, 'OTBFUTR', NULL, NULL, NULL, 'Yes', NULL),
    (24019969, 'BDWCNFG', NULL, NULL, NULL, 'Yes', NULL),
    (24019969, 'OTBCUST', NULL, NULL, NULL, 'Yes', NULL),
    (24019969, 'RESOLVE', NULL, NULL, NULL, 'Yes', NULL),
    (24019969, 'FBRWORK', NULL, NULL, NULL, 'Yes', NULL),
    (24019969, 'BDWMSPR', NULL, NULL, NULL, 'Yes', NULL),
    (24019969, 'BDWPLAN', NULL, NULL, NULL, 'Yes', NULL),
    (180614, 'BDWROUT', 'Yes', NULL, NULL, NULL, NULL),
    (180614, 'BDWCNFG', 'Yes', NULL, NULL, NULL, NULL),
    (23893585, 'RESOLVE', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'LOCSCHD', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'CSPWORK', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'BDWPLAN', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'LOCINST', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'LOCCLOS', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'OTBNGOM', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'OTBTSKD', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'BDWXNG', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'OTBFUTR', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'BDWROUT', NULL, NULL, 'Yes', NULL, NULL),
    (23893585, 'BDWCNFG', NULL, NULL, 'Yes', NULL, NULL)
;

我的尝试是:

Select Distinct X.ID,X.Queue,
case when X.Queue in ('IPTEST','IPPRV','IPPRECF','IPPMOM','IPCON','IPCFG','BDWXNG','LOCCLOS','BDWCNFG','BDWROUT')
and X.Queue not Like '%FBR%' and X.Queue not Like '%CVL%' and X.Queue not Like '%CSP%'
then 'Yes'else 'Null' end AS EER,
case when EER = 'Null' and X.Queue not in ('CSPWORK','CSPRACK','CSPEQUP') and X.Queue not Like '%FBR%' and X.Queue not Like '%CVL%'
then 'Yes'else 'Null' end AS EES,
case when EER = 'Null' and EES = 'Null' and X.Queue Like '%CSP%' and X.Queue not Like '%FBR%' and X.Queue not Like '%CVL%'
then 'Yes'else 'Null' end AS EEN,
case when EER = 'Null' and EES = 'Null' and EEN= 'Null'and X.Queue not Like '%CVL%'
then 'Yes'else 'Null' end AS EBNP,
case when EER = 'Null' and EES = 'Null' and EEN= 'Null' and EBNP = 'Null' and X.Queue Like '%CVL%'
then 'Yes'else 'Null' end AS NSB

From X

实际结果-

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试以下操作(更新的查询):

;with [data] as (
    select [id], [queue] from [Table1] group by [id], [queue]
)
,[eer] as ( 
    select
         [id]       =   [x].[id]
        ,[queue]    =   [x].[queue]
        ,[eer]      =   [eer].[eer]
    from
        [data]  as  [x]
    outer apply (
        select [eer]    = iif(      (exists (select null from [data] where [id] = [x].[id] and [queue] in ('IPTEST','IPPRV','IPPRECF','IPPMOM','IPCON','IPCFG','BDWXNG','LOCCLOS','BDWCNFG','BDWROUT')))
                                and (not exists(select null from [data] where [id] = [x].[id] and ([queue] like '%FBR%' or [queue] like '%CVL%' or [queue] like '%CSP%')))
                            , 1, 0
                        )
    )               as  [eer]
)
,[ees] as ( 
    select
         [id]       =   [x].[id]
        ,[queue]    =   [x].[queue]
        ,[eer]      =   [x].[eer]
        ,[ees]      =   [ees].[ees]
    from
        [eer]   as  [x]
    outer apply (
        select [ees]     = iif(     [x].[eer] = 0
                                and (not exists (select null from [data] where [id] = [x].[id] and [queue] in ('CSPWORK','CSPRACK','CSPEQUP')))
                                and (not exists(select null from [data] where [id] = [x].[id] and ([queue] like '%FBR%' or [queue] like '%CVL%')))
                            , 1, 0
                        )
    )               as  [ees]
)
,[een] as ( 
    select
         [id]       =   [x].[id]
        ,[queue]    =   [x].[queue]
        ,[eer]      =   [x].[eer]
        ,[ees]      =   [x].[ees]
        ,[een]      =   [een].[een]
    from
        [ees]   as  [x]
    outer apply (
        select [een]     = iif(     [x].[eer] = 0 and [x].[ees] = 0
                                and (exists (select null from [data] where [id] = [x].[id] and [queue] like '%CSP%'))
                                and (not exists(select null from [data] where [id] = [x].[id] and ([queue] like '%FBR%' or [queue] like '%CVL%')))
                            , 1, 0
                        )
    )               as  [een]
)
,[ebpn] as ( 
    select
         [id]       =   [x].[id]
        ,[queue]    =   [x].[queue]
        ,[eer]      =   [x].[eer]
        ,[ees]      =   [x].[ees]
        ,[een]      =   [x].[een]
        ,[ebpn]     =   [ebpn].[ebpn]
    from
        [een]   as  [x]
    outer apply (
        select [ebpn]    = iif(     [x].[eer] = 0 and [x].[ees] = 0 and [x].[een] = 0
                                and (not exists (select null from [data] where [id] = [x].[id] and [queue] like '%CVL%'))                               
                            , 1, 0
                        )
    )               as  [ebpn]
)
,[nsb] as ( 
    select
         [id]       =   [x].[id]
        ,[queue]    =   [x].[queue]
        ,[eer]      =   [x].[eer]
        ,[ees]      =   [x].[ees]
        ,[een]      =   [x].[een]
        ,[ebpn]     =   [x].[ebpn]
        ,[nsb]      =   [nsb].[nsb]
    from
        [ebpn]  as  [x]
    outer apply (
        select [nsb]     = iif(     [x].[eer] = 0 and [x].[ees] = 0 and [x].[een] = 0 and [x].[ebpn] = 0
                                and (exists (select null from [data] where [id] = [x].[id] and [queue] like '%CVL%'))                               
                            , 1, 0
                        )
    )               as  [nsb]
)
select 
     [id]       =   [id]            
    ,[queue]    =   [queue] 
    ,[eer]      =   iif([eer]   =   1, 'Yes', null)
    ,[ees]      =   iif([ees]   =   1, 'Yes', null)
    ,[een]      =   iif([een]   =   1, 'Yes', null)
    ,[ebpn]     =   iif([ebpn]  =   1, 'Yes', null)
    ,[nsb]      =   iif([nsb]   =   1, 'Yes', null)
from  
    [nsb]
order by 
     [id]       asc
    ,[queue]    asc;

答案 1 :(得分:0)

如@HoneyBadger所述,您应该执行IS NULL检查,而不是相等运算符。请注意,没有两个NULL相等,因为NULL只是表示没有值的标记。

所以查询应该是

Select Distinct X.ID,X.Queue,
case when X.Queue in ('IPTEST','IPPRV','IPPRECF','IPPMOM','IPCON','IPCFG','BDWXNG','LOCCLOS','BDWCNFG','BDWROUT')
and X.Queue not Like '%FBR%' and X.Queue not Like '%CVL%' and X.Queue not Like '%CSP%'
then 'Yes'else Null end AS EER,
case when EER is Null and X.Queue not in ('CSPWORK','CSPRACK','CSPEQUP') and X.Queue not Like '%FBR%' and X.Queue not Like '%CVL%'
then 'Yes'else Null end AS EES,
case when EER is Null and EES is Null and X.Queue Like '%CSP%' and X.Queue not Like '%FBR%' and X.Queue not Like '%CVL%'
then 'Yes'else Null end AS EEN,
case when EER is Null and EES is Null and EEN is Null and X.Queue not Like '%CVL%'
then 'Yes'else Null end AS EBNP,
case when EER is Null and EES is Null and EEN is Null and EBNP is Null and X.Queue Like '%CVL%'
then 'Yes'else Null end AS NSB

From X