Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每周几天的SQL数据透视_Sql_Sql Server_Sql Server 2008_Pivot_Pivot Table - Fatal编程技术网

每周几天的SQL数据透视

每周几天的SQL数据透视,sql,sql-server,sql-server-2008,pivot,pivot-table,Sql,Sql Server,Sql Server 2008,Pivot,Pivot Table,我正在尝试获取本周的以下输出- Full Name | Mon | Tue | Wed | Thu | Fri | Sat | Sun Peter Smith | 09:00 - 12:00 | NULL | 08:30 - 13:00 | NULL | NULL | NULL | 10:00 - 12:13 Peter Smith | 13:00 - 17:00 | NULL | 14:30 - 16:00 | NULL | NULL

我正在尝试获取本周的以下输出-

Full Name    | Mon           | Tue  | Wed           | Thu  | Fri  | Sat  | Sun
Peter Smith  | 09:00 - 12:00 | NULL | 08:30 - 13:00 | NULL | NULL | NULL | 10:00 - 12:13
Peter Smith  | 13:00 - 17:00 | NULL | 14:30 - 16:00 | NULL | NULL | NULL | 13:00 - 17:14
Paul Stevens | 09:00 - 12:00 | NULL | 08:30 - 13:00 | NULL | NULL | NULL | 10:00 - 12:13
这是显示从表中提取的打卡日志-

**ClockInLogs**
ID - INT
UserID - INT
ClockDateTimeIn - DateTime
ClockDateTimeOut - DateTime
Status - INT (ClockedIn/ClockedOut)
源数据-

ID | UserID | ClockDateTimeIn         | ClockDateTimeOut        | Status
1  | 10000  | 2013-07-30 13:40:39.913 | 2013-07-30 13:42:20.113 | 0
2  | 10000  | 2013-07-30 14:13:10.947 | 2013-07-30 14:25:15.570 | 0
3  | 10001  | 2013-07-30 14:13:52.817 | 2013-07-30 14:25:19.063 | 0
全名从名为Users的联接表中提取

你知道我怎样才能得到想要的输出吗?我知道我需要做一个透视,但无法计算如何在单元格中显示时间

谢谢

declare @test table (ID int, UserID int, ClockDateTimeIn datetime, ClockDateTimeOut datetime, [Status] bit)
declare @user table (ID int, Name nvarchar(128))

insert into @test
select 1, 10000, '2013-07-30 13:40:39.913', '2013-07-30 13:42:20.113', 0 union all
select 2, 10000, '2013-07-30 14:13:10.947', '2013-07-30 14:25:15.570', 0 union all
select 3, 10001, '2013-07-30 14:13:52.817', '2013-07-30 14:25:19.063', 0 union all
select 3, 10001, '2013-07-29 10:13:52.817', '2013-07-30 18:25:19.063', 0

insert into @user
select 10000, 'Martin Smith' union all
select 10001, 'Paul Stevens'

;with CTE as 
(
    select
        u.Name,
        left(datename(weekday, t.ClockDateTimeIn), 3) as Col,
        convert(nvarchar(5), t.ClockDateTimeIn, 108) + ' - ' + convert(nvarchar(5), t.ClockDateTimeOut, 108) as Value,
        row_number() over (partition by u.Name, datename(weekday, t.ClockDateTimeIn) order by t.ClockDateTimeIn) as ID
    from @test as t
        left outer join @user as u on u.ID = t.UserID
)
select P.*
from CTE as C
pivot 
(
    min(Value) for Col in ([Mon], [Tue], [Wen], [Thu], [Fri], [Sat], [Sun])
) as P
order by Name, ID

源数据实际上是什么样子的?我已将源数据添加到问题中。谢谢ClockDateTimeIn和ClockDateTimeOut总是在同一天吗?是的,总是在同一天。如果用户忘记打卡,则会在每天晚上23:30运行脚本,自动打卡打卡的用户。