如何在SQL Server中的查询中执行此操作 [英] How to do it in query in sql server

查看:93
本文介绍了如何在SQL Server中的查询中执行此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

这是我的员工表,下面的数据

Dear all,

Here is my employee table with data below

Id         status             date         time
5001        0               12/09/2011    09:05:34
5002        0               12/09/2011    09:33:13
5001        1               12/09/2011    18:05:09
5002        1               12/09/2011    17:44:34




我想要类似下面的输出




I want the Output like below

Id           date         intime     outtime
5001       12/09/2011    09:05:34    18:05:09
5002       12/09/2011    09:33:13    17:44:34




谢谢<</xml>




Thanks<</xml>

推荐答案

从SQL Server 2005开始,您可以使用
Starting with SQL Server 2005, you can use pivot[^] to bring data from multiple rows into the columns of the output. The link above contains several easy to follow examples.




请尝试以下

Hi,

Try below

SELECT a.Id,a.Date,a.[time] AS intime,b.outtime
FROM   table1 A
       LEFT JOIN (
                SELECT Id,Date,[time] AS outtime
                FROM   table1
                WHERE  table1.status = 1
            ) b ON  a.Id = b.Id
            AND a.date = b.date
WHERE  a.status = 0


这篇关于如何在SQL Server中的查询中执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆