Sql 如果需要,请使用两列

Sql 如果需要,请使用两列,sql,sql-server,Sql,Sql Server,我使用Case When根据两列计算五列。但它没有给出正确的值。我附上了正确结果的截图。 结果需要如下: 业务逻辑是-这五个计算字段具有不同的定义,并且取决于前置字段。EER的定义是,如果一个ID从某个队列中通过,但也没有通过FBR、CVL和CSP等队列。EER字段在一个ID的所有行中都应该有Yes。对于其余四个计算字段,这与case when语句中的方式相同。计算EER后,应计算其余ID的EES。所以我使用EER,EES需要为null。一个ID在五个字段中的一个字段中为是,对于同一ID,在其余

我使用Case When根据两列计算五列。但它没有给出正确的值。我附上了正确结果的截图。 结果需要如下: 业务逻辑是-这五个计算字段具有不同的定义,并且取决于前置字段。EER的定义是,如果一个ID从某个队列中通过,但也没有通过FBR、CVL和CSP等队列。EER字段在一个ID的所有行中都应该有Yes。对于其余四个计算字段,这与case when语句中的方式相同。计算EER后,应计算其余ID的EES。所以我使用EER,EES需要为null。一个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
实际结果-


正如@HoneyBadger所提到的,您应该做的是空检查,而不是相等运算符。注意:没有两个空值是相等的,因为空值只是表示缺少值的标记

因此,查询应该是

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
尝试此(更新的查询):


以上是您的预期结果吗?您似乎忘记包含示例数据,因此我们不知道如何获得该结果。你能提供样品和预期结果吗?首先只计算一列并评估结果,就一列提问。那么我也将为大家简单介绍一下什么输出是不正确的?您给出的值是输入,您给出的SQL与您在输入中给出的EER值不同-这是问题所在吗?第一行中的“LOCCLOS”在您的“in”条件列表中,但它与您要求的任何异常都不一样-因此与“%FBR%”不一样的情况将是真实的-例如-整个事情的评估结果似乎都是真实的-即。它归结为“真与真,真与真”,当您比较
EER='Null'
时,它将变为“真”,但您没有值为
'Null'
的字符串文字,您有一个
NULL
值。检查如下:
EER为空
。谢谢@Juozas,这已向正方向更正。事实上,我正在寻找一个是再次一个ID在需要的结果上面给出。您的代码对一个ID给出了多个“是”。此解决方案-ID队列EER EEN EBNP NSB 180614 BDWCNFG Yes Yes Null Yes Null Null 180614 BDWROUT Yes Null Yes Null Yes Null `我无法理解您的任务。请用简单的语言描述您试图实现的目标:业务逻辑而不是查询。对于要计算的每个列。从您提供的查询中无法理解业务逻辑,因为查询逻辑和期望的结果具有误导性。如果理解不好,我很抱歉。我已经更新了定义并附上了屏幕截图。您的“右”结果屏幕截图中[EER]列有错误。[ID]24048916。[ID]24048916具有[QUEUE]LOCCLO,不包含任何FBR、CVL、CSP。因此,ID 24048916的[ERR]列需要为“Yes”。但你有空。这令人困惑。您的任务很复杂,因此,如果您需要帮助,强烈建议您避免在描述中出现任何错误。@LearnerBee我用新的逻辑更新了我的答案。请使用提供的新查询。请记住,很可能你的结果截图有错误。我真的很感谢你的努力。但这并没有给出正确的结果。我附上了一个正确结果的截图。你的“ansi_NULL”设置为on吗?
;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;