如何找到每个客户的第五个订单并返回title_order
或如果客户没有第五个订单则返回null
表是
customer
列Id, firstname, lastname
... order
列order_id, title_order, id_custmer, date
... 只能通过查询完成,或者我是否需要创建函数
提前致谢
答案 0 :(得分:0)
您可以将OUTER APPLY
与OFFSET-FETCH
:
select c.firstname, oa.title_order
from customer c
outer apply(select title_order from order o
where o.id_custmer = c.Id
order by date
offset 4 ROW FETCH next 1 ROW only)oa