在Sql Server中填充数据(Populate Data in Sql Server)

编程入门 行业动态 更新时间:2024-10-28 18:26:01
在Sql Server中填充数据(Populate Data in Sql Server)

我已经做了很多搜索,并且越来越混淆我的要求。 花了大约2天的时间才找到解决方案。 让我解释一下我的要求

基本上我有一张桌子

---------------------------- | Id | DateLog | TimeLog | |----|------------|----------| | 01 | 2014-06-20 | 15:15:05 | | 01 | 2014-06-20 | 17:43:21 | | 02 | 2014-06-20 | 19:12:10 | | 02 | 2014-06-20 | 13:11:00 |

我想填充上面的表格,就像这样

--------------------------------------- | Id | DateLog | TimeIn | TimeOut | |----|------------|----------|----------| | 01 | 2014-06-20 | 15:15:05 | 17:43:21 | | 02 | 2014-06-20 | 13:11:00 | 19:12:10 |

TimeIn列将基于userId的earlyIn,TimeOut将基于userId的Last Out并按其Id分组。

我尝试做这样的事情,但输出不像我想要的。

select id,DateLog, ( select top 1 Timelog From dbo.[myAttendance] order by TimeLog Asc )AS TimeIn, ( select top(1) TimeLog From dbo.[myAttendance] order by TimeLog Desc )AS TimeOut from dbo.[myAttendance] group by Id

希望你们能在这个问题上帮助我。

I already did a lot of searching and become more and more confuse on the my requirement. Took me about 2 days to find the solution. let me explain what is my requirement

basically i have one table

---------------------------- | Id | DateLog | TimeLog | |----|------------|----------| | 01 | 2014-06-20 | 15:15:05 | | 01 | 2014-06-20 | 17:43:21 | | 02 | 2014-06-20 | 19:12:10 | | 02 | 2014-06-20 | 13:11:00 |

and i want to populate that above table to be similar like this

--------------------------------------- | Id | DateLog | TimeIn | TimeOut | |----|------------|----------|----------| | 01 | 2014-06-20 | 15:15:05 | 17:43:21 | | 02 | 2014-06-20 | 13:11:00 | 19:12:10 |

TimeIn column will be based on the earlyIn of the userId and TimeOut will be based on Last Out of the userId and group them by their Id.

i tried do something like this but the output its not like what i want.

select id,DateLog, ( select top 1 Timelog From dbo.[myAttendance] order by TimeLog Asc )AS TimeIn, ( select top(1) TimeLog From dbo.[myAttendance] order by TimeLog Desc )AS TimeOut from dbo.[myAttendance] group by Id

hopefully you guys can help me on this issue.

最满意答案

在这里,假设Id是用户的ID:

select id, datelog, min(timelog) as TimeIn, max(timelog) as TimeOut from tablename group by id, datelog

Here you are, assuming Id is the User's ID :

select id, datelog, min(timelog) as TimeIn, max(timelog) as TimeOut from tablename group by id, datelog

更多推荐

本文发布于:2023-08-07 16:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465479.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Server   Sql   Populate   Data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!