2014-01-13 112 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) 

當用戶已註銷或仍記錄在此將帶來