SQL根据查询显示Null值的日期

时间:2017-01-03 17:51:13

标签: sql sql-server

我有两个表,employeeemployee time entry。我运行了一个查询,向我显示输入时间总和或0为空值的所有员工。在下一栏我有周数。如果员工在一周内没有输入时间而不是给我0,但它也在周数给我空值。如果员工没有输入,我怎么强制查询才能显示周数。

Select 
    Concat(Empfname,Emplname) as EmployeeName, 
    department,
    iif (sum(whours) is null, 0, sum(whours)) CurrentHours, 
    Datepart (ww,wdate) WeekNum
From 
    employee as e 
left outer join 
    TimeEntry as w on e.id = w.eId 
                   and wdate between '01/01/2017' and '01/31/2017' 
group by
    Concat(Empfname,Emplname), department, Datepart(ww, wdate)

输出

EmployeeName  Department  CurrentHours  WeekNum
------------------------------------------------
John Smith     Sales        8            1
Smith John     Operations   0            Null

我怎么能告诉它也来自WeekNum 1?

由于

2 个答案:

答案 0 :(得分:1)

我们的想法是使用int i = 0; dataArray[i]; i++; 生成所有行,然后使用cross join引入您想要的行:

left join

答案 1 :(得分:0)

试试这个:

Select Concat(Empfname,Emplname) as EmployeeName, department
iif  (sum(whours) is null, 0, sum(whours)) CurrentHours 
ISNULL(Datepart (ww,wdate),1) WeekNum

From employee as e left outer join TimeEntry as w on e.id=w.eId 
 and wdate between '01/01/2017' and '01/31/2017' 

group by Concat(Empfname,Emplname), department, ISNULL(Datepart (ww,wdate),1)

会强制任何NULL值显示1而不是NULL本身

相关问题