我有以下查询:
`
select e.empid, convert(char(5), tr.In_Time, 108) as In_time,
convert(char(5), tr.Out_Time, 108) as Out_time,
convert(varchar(5), sum(datediff(minute, trr.In_Time, isnull(trr.Out_Time, null))) / 60)
+ ':' +
convert(varchar(5),sum(datediff(minute, trr.In_Time, isnull(trr.Out_Time,null))) % 60)
as TotalHours,
from EMPLOYEES e
Left Join EMPLOYEE_TIME tr
on (e.empid=tr.empid)
Left Join EMPLOYEE_TIME trr
on (e.empid=trr.empid)
where (
trr.In_Time BETWEEN '2013-09-11' AND DATEADD(DAY, 1, '2013-09-11')
and tr.In_Time BETWEEN '2013-09-11' AND DATEADD(DAY, 1, '2013-09-11')
) group by e.empid, tr.In_Time, tr.Out_Time e.JoiningDate order by e.JoiningDate ASC
`
执行上述查询后,我得到以下结果: `
EmpID in_time out_time totalhours
1 9:30 18:00 8:30
2 10:00 13:00 8:00
2 14:00 19:00
3 10:30 13:30 3:00
3 14:30 NULL 3:00
`
但是,当多次输入 Out_time为空时,我不想打印 totalhours 两次,如下所示:
`
EmpID in_time out_time totalhours
1 9:30 18:00 8:30
2 10:00 13:00 8:00
2 14:00 19:00
3 10:30 13:30 3:00
3 14:30 NULL
`
有人可以帮帮我吗?提前谢谢
工作SQL
SELECT e.empid ,
CONVERT(CHAR(5), tr.In_Time, 108) AS In_time ,
CONVERT(CHAR(5), tr.Out_Time, 108) AS Out_time ,
CONVERT(VARCHAR(5), SUM(DATEDIFF(minute, trr.In_Time,
ISNULL(trr.Out_Time, NULL))) / 60)
+ ':' + CONVERT(VARCHAR(5), SUM(DATEDIFF(minute, trr.In_Time,
ISNULL(trr.Out_Time, NULL)))
% 60) AS TotalHours
FROM EMPLOYEES e
LEFT JOIN EMPLOYEE_TIME tr ON ( e.empid = tr.empid )
LEFT JOIN EMPLOYEE_TIME trr ON ( e.empid = trr.empid )
WHERE ( trr.In_Time BETWEEN '2013-09-11'
AND DATEADD(DAY, 1, '2013-09-11')
AND tr.In_Time BETWEEN '2013-09-11'
AND DATEADD(DAY, 1, '2013-09-11')
)
GROUP BY e.empid ,
tr.In_Time ,
tr.Out_Time ,
e.JoiningDate
ORDER BY e.JoiningDate ASC
答案 0 :(得分:0)
时只能使用案例
LIKE
CASE WHEN Out_Time IS NOT NULL THEN convert(varchar(5),sum(datediff(minute, trr.In_Time, isnull(trr.Out_Time,null))) % 60)
ELSE NULL END
答案 1 :(得分:0)
根据您的示例,您似乎只想要每个EmpID
的第一行的小时数。 (如果没有,请使用除此之外的其他数据更新问题。)
假设这是一个使用row_number()
的方法:
with yourquery as (<your query here>)
select EmpID, in_time, out_time,
(case when seqnum = 1 then totalhours end) as totalhours
from (select yq.*,
row_number() over (partition by EmpId order by in_time) as seqnum
from yourquery yq
) yq;
编辑:
这应该是这样的:
with yourquery as (
SELECT e.empid ,
CONVERT(CHAR(5), tr.In_Time, 108) AS In_time ,
CONVERT(CHAR(5), tr.Out_Time, 108) AS Out_time ,
CONVERT(VARCHAR(5), SUM(DATEDIFF(minute, trr.In_Time,
ISNULL(trr.Out_Time, NULL))) / 60)
+ ':' + CONVERT(VARCHAR(5), SUM(DATEDIFF(minute, trr.In_Time,
ISNULL(trr.Out_Time, NULL)))
% 60) AS TotalHours
FROM EMPLOYEES e
LEFT JOIN EMPLOYEE_TIME tr ON ( e.empid = tr.empid )
LEFT JOIN EMPLOYEE_TIME trr ON ( e.empid = trr.empid )
WHERE ( trr.In_Time BETWEEN '2013-09-11'
AND DATEADD(DAY, 1, '2013-09-11')
AND tr.In_Time BETWEEN '2013-09-11'
AND DATEADD(DAY, 1, '2013-09-11')
)
GROUP BY e.empid ,
tr.In_Time ,
tr.Out_Time ,
e.JoiningDate
)
select yq.EmpID, yq.in_time, yq.out_time,
(case when seqnum = 1 then yq.totalhours end) as totalhours
from (select yq.*,
row_number() over (partition by yq.EmpId order by in_time) as seqnum
from yourquery yq
) yq
ORDER BY yq.JoiningDate ASC