SQL Server中的CASE语句,带有嵌套选择

时间:2018-01-24 20:24:52

标签: sql sql-server helper

我在SQL Server 2017中遇到了一个问题,我在执行查询时遇到了case语句的查询,它返回了一些空字段,这是我的查询:

select
    case item_value_text
        when 'Low Profile Desktop' then 'Desktop'
        when 'Mini Tower' then 'Desktop'
        when 'All in One' then 'Desktop'
        when 'Portable' then 'Notebook'
        when 'LapTop' then 'Notebook'
        when null then 'No Informado'
        when 'Other' then 'Maquina Virtual'
        else item_value_text
    end as item_value_text
from 
    inv_generalinventory_item GIV
where
    GIV.object_uuid = DHW.dis_hw_uuid and
    item_name_id = 1 anmd 
    item_root_name_id = 1 and
    item_parent_name_id = 2 and
    (item_value_text like 'All in One' or
     item_value_text like 'Bus Expanson chassis' or
     item_value_text like 'Desktop' or
     item_value_text like 'Docking Station' or
     item_value_text like 'Expansion chassis' or
     item_value_text like 'Hand Held' or
     item_value_text like 'Laptop' or
     item_value_text like 'Low Profile Desktop' or
     item_value_text like 'Lunch Box' or
     item_value_text like 'Main Server chassis' or
     item_value_text like 'Mini Tower' or
     item_value_text like 'Notebook' or
     item_value_text like 'Other' or
     item_value_text like 'Peripherical chassis' or
     item_value_text like 'Pizza box' or
     item_value_text like 'Portable' or
     item_value_text like 'RACK Mount chassis' or
     item_value_text like 'RAID chassis' or
     item_value_text like 'Sealed-case PC' or
     item_value_text like 'Space-saving' or
     item_value_text like 'Sub Notebook' or
     item_value_text like 'Subchassis' or
     item_value_text like 'Tower' or
     item_value_text like 'Unknown')) 'Chassis'

我不知道我能做什么,我试试

SELECT 
    CASE ISNULL(ítem_value_text, 'NULL')
       WHEN 'NULL' THEN 'No Informado'
       ELSE ítem_value_text
    END

它也不起作用,对于内部也是如此:

WHEN ISNULL(ítem_value_text,'NULL') 
   THEN 'No Informado'

我该怎么办?

4 个答案:

答案 0 :(得分:0)

试试这个

case isnull(item_value_text,'x')
when 'Low Profile Desktop' then 'Desktop'
when 'Mini Tower' then 'Desktop'
when 'All in One' then 'Desktop'
when 'Portable' then 'Notebook'
when 'LapTop' then 'Notebook'
WHEN 'x' THEN 'No Informado'
when 'Other' then 'Maquina Virtual'
else item_value_text
end as item_value_text
使用IN

和你的where子句应如下所示

从没有通配符的原始代码中获取类似于' ='但由于你有这么多值要比较,所以IN是合适的

where
GIV.object_uuid = DHW.dis_hw_uuid and
 item_name_id=1 AND
 item_root_name_id=1 AND
 item_parent_name_id=2 AND
 item_value_text in ('All in One','....',....)

答案 1 :(得分:0)

简化:

case  item_value_text   
when 'Low Profile Desktop' then 'Desktop'
when 'Mini Tower' then 'Desktop'
when 'All in One' then 'Desktop'
when 'Portable' then 'Notebook'
when 'LapTop' then 'Notebook'
when 'Other' then 'Maquina Virtual'
else isnull(item_value_text,'No Informado')
end as item_value_text

答案 2 :(得分:0)

'NULL'NULL不同。 'NULL'是由4个字符组成的字符串文字。要在SQL中测试未知值,您必须使用IS NULL(或IS NOT NULL)。

item_value_text like 'Tower' or
item_value_text IS NULL

答案 3 :(得分:0)

尝试更改您的案例陈述,如下所示

case
        when item_value_text='Low Profile Desktop' then 'Desktop'
        when item_value_text='Mini Tower' then 'Desktop'
        when item_value_text='All in One' then 'Desktop'
        when item_value_text='Portable' then 'Notebook'
        when item_value_text='LapTop' then 'Notebook'
        when item_value_text  is null then 'No Informado'
        when item_value_text='Other' then 'Maquina Virtual'