如果我们按日期asc组织结果,那么我有一行checkIn
,第一行是Checkout
,第二行是Checkout
。
我需要将第二行值传递给另一个名为{{1}}的列。此表至少有1000条记录
答案 0 :(得分:0)
您可以使用row_number()
:
select t.*
from (select t.*,
row_number() over (partition by studentid order by transactiondate) as seqnum
from t
) t
where seqnum = 2;
这假设只使用studentid
来识别"第一个"和"第二"行。如果需要更多列,请将它们添加到partition by
子句中。
答案 1 :(得分:0)
如果您需要的结帐日期是按原因分组的第二个(最长)日期,并且您想要单独列出此日期,请尝试:
select
studentID,
reason,
max(transactionDate) as checkoutDate
from student
group by
reason
答案 2 :(得分:0)
尝试使用以下查询
With CteCheckOut as(
Select
ROW_NUMBER() over (Partition by studentID Order by studentID,transactionDate) as Rownumber,
*
From student
)
Select studentID, transactionDate as CheckOutDate
From CteCheckOut
Where Rownumber%2 = 0