在Case语句中获取错误SQL获取NULL

时间:2018-04-02 11:17:35

标签: sql sql-server

我使用SQL在报表生成器中获取查询,我已将case语句放在sql查询中,但我在月份col中得到NULL

    select * into #temp from
(select count(*) [Total Clients], li.title,
 SUBSTRING(li.title,CHARINDEX('_',li.title,CHARINDEX('_',li.title)+1)+1,2) as 'month1',
 CASE WHEN 'month1' = '01' THEN 'Jan'
            WHEN 'month1' = '02' THEN 'Feb'
            WHEN 'month1' = '03' THEN 'Mar'
            WHEN 'month1' = '04' THEN 'Apr'
            WHEN 'month1' = '05' THEN 'May'
            WHEN 'month1' = '06' THEN 'June'
            WHEN 'month1' = '07' THEN 'Jul'
            WHEN 'month1' = '08' THEN 'Aug'
            WHEN 'month1' = '09' THEN 'Sep'
            WHEN 'month1' = '10' THEN 'Oct'
            WHEN 'month1' = '11' THEN 'Nov'
            WHEN 'month1' = '12' THEN 'Dec'
            END As [Month],
li.CI_UniqueID,coll.name,coll.CollectionID,
SUM (CASE WHEN ucs.status=3 or ucs.status=1  then 1 ELSE 0 END ) as 'Installed / Not Applicable',
sum( case When ucs.status=2 Then 1 ELSE 0 END ) as 'Required',
sum( case When ucs.status=0 Then 1 ELSE 0 END ) as 'Unknown',
round((CAST(SUM (CASE WHEN ucs.status=3 or ucs.status=1 THEN 1 ELSE 0 END) as float)/count(*) )*100,2) as 'Compliant%',
    round((CAST(count(case when ucs.status not in('3','1') THEN '*' end) as float)/count(*))*100,2) as 'NotCompliant%'
    From v_Update_ComplianceStatusAll UCS
inner join v_r_system sys on ucs.resourceid=sys.resourceid
inner join v_FullCollectionMembership fcm on ucs.resourceid=fcm.resourceid
inner join v_collection coll on coll.collectionid=fcm.collectionid
inner join v_AuthListInfo LI on ucs.ci_id=li.ci_id
where coll.CollectionID like '%SMS00001%' and
--title like '%SUG%' 
 Title like '%P1%' and
Title like '%SUG_' + '' + CAST(year(getdate()) as varchar) +  '' + '%'
--or Title like '%SUG_' + '' + CAST(year(getdate())-1 as varchar) +  '' + '%'
group by li.title,li.CI_UniqueID,coll.name,coll.CollectionID) as e

这是查询输出iam获得的是月份col中的NULL值

请找图片

enter image description here

你可以帮忙解释为什么我在COL COL

中获得NULL

3 个答案:

答案 0 :(得分:4)

您可以尝试这样的事情,

select count(*) [Total Clients], li.title,
 SUBSTRING(li.title,CHARINDEX('_',li.title,CHARINDEX('_',li.title)+1)+1,2) as 'month1',
 CASE SUBSTRING(li.title,CHARINDEX('_',li.title,CHARINDEX('_',li.title)+1)+1,2) 
            WHEN '01' THEN 'Jan'
            WHEN '02' THEN 'Feb'
            WHEN '03' THEN 'Mar'
            WHEN '04' THEN 'Apr'
            WHEN '05' THEN 'May'
            WHEN '06' THEN 'June'
            WHEN '07' THEN 'Jul'
            WHEN '08' THEN 'Aug'
            WHEN '09' THEN 'Sep'
            WHEN '10' THEN 'Oct'
            WHEN '11' THEN 'Nov'
            WHEN '12' THEN 'Dec'
            END As [Month],
li.CI_UniqueID,coll.name,coll.CollectionID ....

或者在外部查询中处理它

答案 1 :(得分:0)

您的案例expression将是

(case (SUBSTRING(li.title,CHARINDEX('_',li.title,CHARINDEX('_',li.title)+1)+1,2))
    when '01' then 'Jan'
    ... 
 end)

您正在使用alise作为列名

答案 2 :(得分:0)

您可以将CASE语句替换为:

 case when SUBSTRING(li.title,CHARINDEX('_',li.title,CHARINDEX('_',li.title)+1)+1,2) between '01' and '12' 
      then convert( char(3),cast( '2018/'+ SUBSTRING(li.title,CHARINDEX('_',li.title,CHARINDEX('_',li.title)+1)+1,2) +'/01' as datetime),0)  
 end as [MONTH],