declare @T table (userid int, TransactionTime datetime, FunctionKey int)
insert into @T values
(1018, '2011-07-27 09:14:38.000', 0),
(1018, '2011-07-27 09:17:11.000', 99),
(1018, '2011-07-27 09:38:22.000', 0),
(1018, '2011-07-27 10:34:50.000', 99),
(1019, '2011-07-27 09:14:38.000', 0),
(1019, '2011-07-27 09:17:11.000', 0),
(1019, '2011-07-27 09:38:22.000', 99),
(1020, '2011-07-27 09:14:38.000', 0),
(1020, '2011-07-27 09:17:11.000', 99),
(1020, '2011-07-27 09:38:22.000', 99)
;with cte as
(
select userid,
TransactionTime,
FunctionKey,
row_number() over(partition by userid order by TransactionTime) as rn
from @T
)
select C1.userid,
C1.TransactionTime as InTime,
C2.TransactionTime as OutTime
from cte as C1
left outer join cte as C2
on C1.userid = C2.userid and
C1.rn + 1 = C2.rn
where C1.FunctionKey = 0
結果:
userid InTime OutTime
----------- ----------------------- -----------------------
1018 2011-07-27 09:14:38.000 2011-07-27 09:17:11.000
1018 2011-07-27 09:38:22.000 2011-07-27 10:34:50.000
1019 2011-07-27 09:14:38.000 2011-07-27 09:17:11.000
1019 2011-07-27 09:17:11.000 2011-07-27 09:38:22.000
1020 2011-07-27 09:14:38.000 2011-07-27 09:17:11.000
當Funtionkey爲0時,它的銀泰,當它是99,然後是它的運行時間 –
你是否需要處理'假'數據? (同時登錄和註銷,登錄後登錄,註銷後註銷等等) – MatBailie
爲什麼在這個問題上downvotes!?!? – MatBailie