有人请帮助我,理清我的时间和出勤SQL查询 我有一个SQL表,其中包含字段ID,日期,时间,StaffNo,TranType(这将是两个值1,2 1 = 2 = out)我想要输出如此id日期StaffNo及时输出时间
答案 0 :(得分:0)
这是你要找的吗?
表格声明
create table #tab (id int primary key ,[date] date,[time] varchar(10),staffno int,trantype int)
insert into #tab values (1,'2013-01-01','12:31:09',101,1),(2,'2013-01-01','12:51:09',101,2),(3,'2013-01-01','09:31:09',102,1)
select * from #tab
示例代码根据我的理解要求。
select
x.date,x.StaffNo,max(x.timeIn) timeIn,max(x.timeOut) timeOut
from
(
select
StaffNo,[date],
timeIn = (case when trantype = 1 then ([time]) else '' end),
timeOut = (case when trantype = 2 then ([time]) else '' end)
from
#tab
) x
group by
x.date,x.StaffNo