Sql查询中的时间日志透视
我一直在查询我的timelogs表, 以下是示例数据:Sql查询中的时间日志透视,sql,sql-server-2008-r2,Sql,Sql Server 2008 R2,我一直在查询我的timelogs表, 以下是示例数据: TimelogId EmployeeId RecordDate RecordTime Type --------- ---------- ---------- ---------- ---- 1 4 2016-07-01 07:18:37 1 2 4 20
TimelogId EmployeeId RecordDate RecordTime Type
--------- ---------- ---------- ---------- ----
1 4 2016-07-01 07:18:37 1
2 4 2016-07-01 12:03:14 2
5 4 2016-07-01 12:08:02 1
6 4 2016-07-01 18:02:03 2
7 6 2016-07-19 07:24:15 1
8 5 2016-07-19 07:26:03 1
9 6 2016-07-19 12:15:26 2
10 5 2016-07-19 12:35:17 2
13 5 2016-07-19 12:36:14 1
16 6 2016-07-19 12:45:45 1
17 6 2016-07-19 17:10:22 2
18 5 2016-07-19 18:43:09 2
所需输出:
Date EmployeeId Time_In Time_Out Time_In Time_Out
------- ---------- -------- -------- ------- -------
2016-07-01 4 07:18:37 12:03:14 12:08:03 18:02:03
2016-07-19 6 07:24:15 12:15:26 12:45:45 17:10:22
2016-07-19 5 07:26:03 12:35:17 12:36:14 18:43:08
其中类型
(1)=超时,而类型
(2)=超时
员工需要在12点注销,然后在几分钟后重新登录
我试着使用我在这里读到的基于pivot的问题。虽然这是我第一次尝试在表中使用pivot
select
*
FROM
(
select
EmployeeID,
RecordDate,
RecordTime,
Type
from tblTimeLogs
) d
PIVOT(
Max(RecordTime) <---- I think the problem is right around here
FOR Type in ([1],[2]) <----- plus i can't seem to rename this column names maybe i'll read a little further on this.
) piv;
这可能吗?谢谢D
编辑:
正如vercelli所建议的,另一个场景,例如,用户忘记他/她在几分钟前已经计时,所以她再次计时。
e、 g
新查询的输出:
Emp RecordDate Time_In1 Time_Out1 Time_In2 Time_Out2
---- ---------- --------- --------- -------- ---------
4 2016-07-01 07:18:37 12:03:14 12:08:02 18:02:03
5 2016-07-19 07:26:03 12:35:17 12:36:14 18:43:09
5 2016-07-20 08:13:35 08:14:35 12:15:12 Null <--- the problem is right around this portion
6 2016-07-19 07:24:15 12:15:26 12:45:45 17:10:22
Emp RecordDate Time\u in 1 Time\u out 1 Time\u in 2 Time\u out 2
---- ---------- --------- --------- -------- ---------
4 2016-07-01 07:18:37 12:03:14 12:08:02 18:02:03
5 2016-07-19 07:26:03 12:35:17 12:36:14 18:43:09
5 2016-07-20 08:13:35 08:14:35 12:15:12空我想这就是你要找的
输出
Emp RecordDate Time_In1 Time_Out1 Time_In2 Time_out2
4 2016-07-01 07:18:37 12:03:14 12:08:02 18:02:03
5 2016-07-19 07:26:03 12:35:17 12:36:14 18:43:09
6 2016-07-19 07:24:15 12:15:26 12:45:45 17:10:22
我想这就是你要找的
输出
Emp RecordDate Time_In1 Time_Out1 Time_In2 Time_out2
4 2016-07-01 07:18:37 12:03:14 12:08:02 18:02:03
5 2016-07-19 07:26:03 12:35:17 12:36:14 18:43:09
6 2016-07-19 07:24:15 12:15:26 12:45:45 17:10:22
---表创建脚本
create table #test
(
employeeid int,
recorddate date,
recordtime time,
typee int
)
insert into #test values( 4 , '2016-07-01', '07:18:37', 1)
insert into #test values( 4 , '2016-07-01', '12:03:14', 2)
insert into #test values( 4 , '2016-07-01', '12:08:02', 1)
insert into #test values( 4 , '2016-07-01', '18:02:03', 2)
质疑
---表创建脚本
create table #test
(
employeeid int,
recorddate date,
recordtime time,
typee int
)
insert into #test values( 4 , '2016-07-01', '07:18:37', 1)
insert into #test values( 4 , '2016-07-01', '12:03:14', 2)
insert into #test values( 4 , '2016-07-01', '12:08:02', 1)
insert into #test values( 4 , '2016-07-01', '18:02:03', 2)
质疑
我得到timeout>timein1,应该是另一个way@vercelli:更新支票now@TheGameiswar:感谢您的回答,我已尝试过该查询,但它输出[Err]42000-[SQL Server]“lead”不是可识别的内置函数名。可能是由于不兼容问题。我在微软网站上读到这个函数是从2012年及以上版本开始内置的。我得到的是timeout>timein1,应该是另一个way@vercelli:更新支票now@TheGameiswar:感谢您的回答,我已尝试过该查询,但它输出[Err]42000-[SQL Server]“lead”不是可识别的内置函数名。可能是由于不兼容问题。我在微软网站上读到这个功能是从2012年及以上版本开始内置的。谢谢你的回答。查询是否根据其包含的值过滤结果或行:我试着在这里创建一个例子。例如,用户登录了两次,因为他/她忘记了她刚刚登录。@jkw我明白你的意思,但你会如何显示它?请编辑您的问题以显示此新场景谢谢您的回答。查询是否根据其包含的值过滤结果或行:我试着在这里创建一个例子。例如,用户登录了两次,因为他/她忘记了她刚刚登录。@jkw我明白你的意思,但你会如何显示它?请编辑您的问题以显示此新方案
select
EmployeeID as Emp, RecordDate, [1] as Time_In1, [2] as Time_Out1, [3] as Time_In2, [4] as Time_out2
FROM
(
select
EmployeeID,
RecordDate,
RecordTime,
ROW_NUMBER () over (partition by EmployeeId, RecordDate order by RecordTime, type) as rn
from tblTimeLogs
) d
PIVOT(
max(RecordTime)
FOR rn in ([1],[2],[3],[4])
) as piv;
Emp RecordDate Time_In1 Time_Out1 Time_In2 Time_out2
4 2016-07-01 07:18:37 12:03:14 12:08:02 18:02:03
5 2016-07-19 07:26:03 12:35:17 12:36:14 18:43:09
6 2016-07-19 07:24:15 12:15:26 12:45:45 17:10:22
create table #test
(
employeeid int,
recorddate date,
recordtime time,
typee int
)
insert into #test values( 4 , '2016-07-01', '07:18:37', 1)
insert into #test values( 4 , '2016-07-01', '12:03:14', 2)
insert into #test values( 4 , '2016-07-01', '12:08:02', 1)
insert into #test values( 4 , '2016-07-01', '18:02:03', 2)
;with cte
as
(
select
employeeid,
max(recorddate) as recorddate,
min(recordtime) as timein,
max(recordtime) as Timein1 ,
lead(min(recordtime)) over (partition by employeeid order by min(recordtime)) as 'timeout1',
lead(max(recordtime)) over (partition by employeeid order by max(recordtime)) as 'timeout2'
from #test
group by
employeeid,typee
)
select employeeid,recorddate,timein,timeout1,timein1,timeout2
from cte
where timeout1 is not null and timeout2 is not null