为null表示无效的列名错误

问题描述 投票:1回答:1

我有这样的查询:

DECLARE @stardate date = '31 OCT 2019'  ,
@endate date = '31 OCT 2019' ,
@emp_id int = 0,
@id int=1


DECLARE @lateVal as TIME
SET @lateVal = '08:15' -- Set Late Variable Here

;WITH calendar (FromDate) AS (
    SELECT @stardate AS FromDate
    UNION ALL
    SELECT DATEADD(day, 1, FromDate)
    FROM Calendar
    WHERE FromDate < @endate
) 


,
CTE AS 
(
select U.Name,U.Adluserid as 'Empid', cast(C.FromDate as date) AS [Date],
       min(case when IOType=0 then Edatetime end) as 'IN',
       max(case when IOType=1 then Edatetime end) as 'out',
       (case when max(Tr.Status)=1 then (select  Description from Tbl_Zeo_Status where Id=@id) when max(Tr.Status)=2 then 'Absent as Sick' else 'L' end) as leave_status,
       TS.Description,
       Tr.Status as statusid

       from calendar C

cross join Mx_UserMst U --on Trn.UsrRefcode=U.UserID
left join Mx_ACSEventTrn Trn  on cast(Edatetime as date)=cast(C.FromDate as date)  AND  Trn.UsrRefcode=U.AdlUserID
left join Tbl_Zeo_Empstatus E on  Trn.UsrRefcode=E.Emp_Id
left join Tbl_Zeo_EmpTrans Tr on Trn.UsrRefcode=Tr.Emp_Id and cast(Tr.Date as date)=cast(Edatetime as date)
left join Tbl_Zeo_Status TS on Tr.Status= Ts.Id
where cast(C.FromDate as date) between  @stardate and @endate  
 and (@emp_id=0 OR E.Emp_Id=@emp_id) and U.Name is not null and  U.UserIDEnbl = 1
group by U.Name,U.Adluserid , C.FromDate ,ts.Description,Tr.Status
)
 SELECT  ROW_NUMBER() OVER(ORDER BY name ASC) AS [id],[Empid],[Name],[IN],[OUT],
 (case when cast([IN] as time) is null then cast(leave_status as nvarchar(50))
 when cast([IN] as time) < @lateVal then 'Present' else 'Late' end) as status,[Description],
 [statusid]

 FROM CTE
 order by [Name],[IN]

如果我的描述列为空,我想显示状态列的值,所以我这样编辑查询:

ISNULL( [Description],[status]) as Des

但显示错误无效的列名称'状态'。

为什么这样显示

sql isnull
1个回答
1
投票

您可以使用subquery获得该值。

;WITH calendar (FromDate) AS (
    SELECT @stardate AS FromDate
    UNION ALL
    SELECT DATEADD(day, 1, FromDate)
    FROM Calendar
    WHERE FromDate < @endate
) 
,
CTE AS 
(
select U.Name,U.Adluserid as 'Empid', cast(C.FromDate as date) AS [Date],
       min(case when IOType=0 then Edatetime end) as 'IN',
       max(case when IOType=1 then Edatetime end) as 'out',
       (case when max(Tr.Status)=1 then (select  Description from Tbl_Zeo_Status where Id=@id) when max(Tr.Status)=2 then 'Absent as Sick' else 'L' end) as leave_status,
       TS.Description,
       Tr.Status as statusid
       from calendar C
cross join Mx_UserMst U --on Trn.UsrRefcode=U.UserID
left join Mx_ACSEventTrn Trn  on cast(Edatetime as date)=cast(C.FromDate as date)  AND  Trn.UsrRefcode=U.AdlUserID
left join Tbl_Zeo_Empstatus E on  Trn.UsrRefcode=E.Emp_Id
left join Tbl_Zeo_EmpTrans Tr on Trn.UsrRefcode=Tr.Emp_Id and cast(Tr.Date as date)=cast(Edatetime as date)
left join Tbl_Zeo_Status TS on Tr.Status= Ts.Id
where cast(C.FromDate as date) between  @stardate and @endate  
 and (@emp_id=0 OR E.Emp_Id=@emp_id) and U.Name is not null and  U.UserIDEnbl = 1
group by U.Name,U.Adluserid , C.FromDate ,ts.Description,Tr.Status
)
select id, EmpId, [Name], [IN], [OUT], [status], isnull([Description], [status]) from ( select ROW_NUMBER() OVER(ORDER BY name ASC) AS [id],[Empid],[Name],[IN],[OUT],
 (case when cast([IN] as time) is null then cast(leave_status as nvarchar(50))
    when cast([IN] as time) < @lateVal then 'Present' else 'Late' end) as status, [Description],
 [statusid]
 FROM CTE) t1
 order by [Name],[IN]

,您需要重复case语句。

isnull([Description],(case when cast([IN] as time) is null then cast(leave_status as nvarchar(50))
    when cast([IN] as time) < @lateVal then 'Present' else 'Late' end))
© www.soinside.com 2019 - 2024. All rights reserved.