具有多行输出的 SQL Server 数据透视
我的表中有以下数据。
我需要以下格式的输出。
我尝试过旋转,但似乎无法解决它。 有人可以在这里指导我吗?
提前致谢。
编辑: 文本格式的数据。
员工ID | 轮班代码 | 出勤日期 | 输入日期 | 超时日期时间 | 加班时间 |
---|---|---|---|---|---|
26 | ShiftCC1 | 01-03-2022 | 01-03-2022 09:10 | 01-03-2022 18:10 | 1 |
26 | ShiftCC1 | 02-03-2022 | 02-03-2022 09:15 | 02-03-2022 18:15 | 2 |
26 | ShiftCC1 | 03-03-2022 | 03-03-2022 09:05 | 03-03-2022 18:05 | 2 |
26 | ShiftCC1 | 04-03-2022 | 04-03-2022 09:10 | 04-03-2022 18:10 | 1 |
26 | ShiftCC1 | 05-03-2022 | 05-03-2022 09:13 | 05-03-2022 18:13 | 2 |
26 | ShiftCC1 | 06-03-2022 | 06-03-2022 09:14 | 06-03-2022 18:14 | 3 |
26 | ShiftCC1 | 07-03-2022 | 07-03-2022 09:16 | 07-03-2022 18:16 | 2 |
26 | ShiftCC1 | 08-03-2022 | 08-03-2022 09:30 | 08-03-2022 18:30 | 1 |
26 | ShiftCC1 | 09-03-2022 | 09-03-2022 09:20 | 09-03-2022 18:20 | 2 |
26 | ShiftCC1 | 10-03-2022 | 10-03-2022 09:25 | 10-03-2022 18:25 | 3 |
以文本格式输出:
EmployeeID | ShiftCode | DataType | 01-03-2022 | 02-03-2022 | 03-03-2022 | 04-03-2022 | 05-03-2022 | 06-03-2022 | 07-03 -2022年 | 08-03-2022 | 09-03-2022 | 10-03-2022 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
26 | ShiftCC1 | InDateTime | 01-03-2022 09:10 | 02-03-2022 09:15 | 03-03-2022 09:05 | 04-03-2022 09:10 | 05-03-2022 09:13 | 06-03-2022 09:14 | 07-03-2022 09:16 | 08-03-2022 09:30 | 09-03-2022 09:20 | 10-03-2022 09:25 |
26 | ShiftCC1 | 过时时间 | 01-03-2022 18:10 | 02-03-2022 18:15 | 03-03-2022 18:05 | 04-03-2022 18:10 | 05-03-2022 18:13 | 06-03-2022 18:14 | 07- 03-2022 18:16 | 08-03-2022 18:30 | 09-03-2022 18:20 | 10-03-2022 18:25 |
26 | ShiftCC1 | 加班时间 | 1 | 2 | 2 | 1 | 2 | 3 | 2 | 1 | 2 | 3 |
I have the following data in a Table.
And i need output in below format.
I tried pivoting but seems like not able to resolve it.
Can someone please guide me here?
Thanks in advance.
Edit:
Data in Text Format.
EmployeeID | ShiftCode | AttendanceDate | inDateTime | outDateTime | OverTimeHours |
---|---|---|---|---|---|
26 | ShiftCC1 | 01-03-2022 | 01-03-2022 09:10 | 01-03-2022 18:10 | 1 |
26 | ShiftCC1 | 02-03-2022 | 02-03-2022 09:15 | 02-03-2022 18:15 | 2 |
26 | ShiftCC1 | 03-03-2022 | 03-03-2022 09:05 | 03-03-2022 18:05 | 2 |
26 | ShiftCC1 | 04-03-2022 | 04-03-2022 09:10 | 04-03-2022 18:10 | 1 |
26 | ShiftCC1 | 05-03-2022 | 05-03-2022 09:13 | 05-03-2022 18:13 | 2 |
26 | ShiftCC1 | 06-03-2022 | 06-03-2022 09:14 | 06-03-2022 18:14 | 3 |
26 | ShiftCC1 | 07-03-2022 | 07-03-2022 09:16 | 07-03-2022 18:16 | 2 |
26 | ShiftCC1 | 08-03-2022 | 08-03-2022 09:30 | 08-03-2022 18:30 | 1 |
26 | ShiftCC1 | 09-03-2022 | 09-03-2022 09:20 | 09-03-2022 18:20 | 2 |
26 | ShiftCC1 | 10-03-2022 | 10-03-2022 09:25 | 10-03-2022 18:25 | 3 |
output in Text Format:
EmployeeID | ShiftCode | DataType | 01-03-2022 | 02-03-2022 | 03-03-2022 | 04-03-2022 | 05-03-2022 | 06-03-2022 | 07-03-2022 | 08-03-2022 | 09-03-2022 | 10-03-2022 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
26 | ShiftCC1 | InDateTime | 01-03-2022 09:10 | 02-03-2022 09:15 | 03-03-2022 09:05 | 04-03-2022 09:10 | 05-03-2022 09:13 | 06-03-2022 09:14 | 07-03-2022 09:16 | 08-03-2022 09:30 | 09-03-2022 09:20 | 10-03-2022 09:25 |
26 | ShiftCC1 | OutDateTime | 01-03-2022 18:10 | 02-03-2022 18:15 | 03-03-2022 18:05 | 04-03-2022 18:10 | 05-03-2022 18:13 | 06-03-2022 18:14 | 07-03-2022 18:16 | 08-03-2022 18:30 | 09-03-2022 18:20 | 10-03-2022 18:25 |
26 | ShiftCC1 | OverTimeHours | 1 | 2 | 2 | 1 | 2 | 3 | 2 | 1 | 2 | 3 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的数据集无法获得您想要的准确输出。您要么需要放弃加班列,因为在旋转这些列后会自动转换为日期类型,要么您可以更改列的数据类型(我为此使用了视图)。
另外,我假设您的“AttendanceDate”是动态的,所以我使用了动态解决方案。
我在这里使用了 @Taryn 的解决方案:
选项 1:
DBFiddle_Option1:
或
Option2:
您可以创建视图这样可以绕过列不兼容。
然后你可以使用这个:
DBFiddle_Option2:
注意:不将数据转换为字符串,最后看起来像这样:
First of all, you can' t have the exactly output you want with your dataset. Either you need to give up your overtime column because after pivotting those columns automatically convert to date type or you could change datatype of your columns(I used view for that).
Also, I assumed your "AttendanceDate" is dynamic, so I used a dynamic solution.
I used @Taryn' s solution here:
Option 1:
DBFiddle_Option1:
or
Option2:
You can create a view like this to bypass column incompatibility.
Then you can use this:
DBFiddle_Option2:
Note: without converting data to string it looks like this at the end: