2014-01-13 110 views
0

我米具有这样的数据...如何在SQL Server 2012

sample data

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 

的InOut 1显示在单独的列行数据 - “在定时” 和2“ - 出定时”。我希望数据在单独列中按照“定时和输出定时”排序。我米使用在2012年SQL上述查询,以显示像下面

获得输出 Getting Output

期望输出 Requested Image

在它仅显示前两行输出的数据。由于第三行中没有出现时机2,因此不显示第三行。请告诉我如何更正代码。

回答

0

简单,我得到这个通过修改内部联接到左的连接,并在condtion

0

我认为这个问题是与where子句删除o.InOUt = 2。试试这个where子句来代替:

where (i.InOUt = 1) 
and (o.InOUt = 2 or o.InOUt is null) 

当用户已注销或仍记录在此将带来