Sql server 2012 如何在sql server 2012中以单独的列显示行数据

Sql server 2012 如何在sql server 2012中以单独的列显示行数据,sql-server-2012,Sql Server 2012,我有这样的数据 with cte as (select *, rn = row_number() over (partition by empid order by trtime) from [10.xx.xx.xx].[dbName].dbo.[tableName] where empid='00ec2137' and trdate='01/13/2014' ) select i.empid,i.trdate, i.trtime InTime, o.trtime OutTi

我有这样的数据

with cte as
(select    *, rn = row_number() over (partition by empid order by trtime)
 from [10.xx.xx.xx].[dbName].dbo.[tableName] where empid='00ec2137' and    trdate='01/13/2014'
)
select    i.empid,i.trdate, i.trtime InTime, o.trtime OutTime
from    cte i
 inner join cte o    on    i.empid    = o.empid
             and    i.rn     = o.rn - 1

where i.InOUt    = 1
and    o.InOUt = 2
order by i.empid,i.rn
输入输出1输入定时和2输出定时。我希望数据像在一个单独的列中定时和定时一样进行排序。我在SQL 2012中使用上述查询来显示如下数据

获取输出

预期产量


在输出中,它仅显示前两行。由于第三行中没有输出正时2,因此不显示第三行。请建议我如何更正代码。

很简单,我是通过将内部连接修改为左连接并删除where条件中的o.InOUt=2来实现的。我认为问题在于where子句。请尝试以下where子句:

where (i.InOUt    = 1)
and    (o.InOUt = 2 or o.InOUt is null)
当用户已注销或仍在登录时,这将带来