我正在使用一个查询来查询员工每周的总工时。我在select中使用select将周分隔为列。我收到一些雇员的空票。如何删除空值
select distinct
EM.EMPLOYEE,
RTRIM(LTRIM(LAST_NAME))+', '+RTRIM(LTRIM(FIRST_NAME)) as [FULL NAME],
(Select SUM(ISNULL(HOURS,'0')) from TimeTbl PT2 WHERE DATEPART(WK, Date) = DATEPART(WK, DATEADD(WK, -1, getdate())) and PAYROLL_YEAR='2020'and PT2.EMPLOYEE=PT.EMPLOYEE) AS [WEEK 1 HOURS],
(Select SUM(ISNULL(HOURS,'0')) from TimeTbl PT2 WHERE DATEPART(WK, Date) = DATEPART(WK, DATEADD(WK, -2, getdate())) and PAYROLL_YEAR='2020'and PT2.EMPLOYEE=PT.EMPLOYEE) AS [WEEK 2 HOURS],
(Select SUM(ISNULL(HOURS,'0')) from TimeTbl PT2 WHERE DATEPART(WK, Date) = DATEPART(WK, DATEADD(WK, -3, getdate())) and PAYROLL_YEAR='2020'and PT2.EMPLOYEE=PT.EMPLOYEE) AS [WEEK 3 HOURS],
(Select SUM(ISNULL(HOURS,'0')) from TimeTbl PT2 WHERE DATEPART(WK, Date) = DATEPART(WK, DATEADD(WK, -4, getdate())) and PAYROLL_YEAR='2020' and PT2.EMPLOYEE=PT.EMPLOYEE)AS [WEEK 4 HOURS],
(Select SUM(ISNULL(HOURS,'0')) from TimeTbl PT2 WHERE DATEPART(WK, TR_DATE) = DATEPART(WK, DATEADD(WK, -5, getdate())) and PAYROLL_YEAR='2020' and PT2.EMPLOYEE=PT.EMPLOYEE)AS [WEEK 5 HOURS]
From TimeTbl PT
right join EMPLYTBL EM on PT.EMPLOYEE=EM.EMPLOYEE
1条答案
按热度按时间guykilcj1#
使用条件聚合: