赞
踩
语法:Row_Number() OVER (partition by 分组字段 order by 排序字段 desc)
select a.*,b.*
from 表A as a
left join
(select id, rightid,Row_Number() over(partition by id order by id desc) as rownum
from 表B) as b
on a.id=b.id
where b.rownum=1 and a.id<>''
说明:无论联接是否存在,都会从外部表和表值函数中返回数据行。这与OUTER JOIN 很类似。如果表值函数中没有行存在,则从函数中返回的列值为NULL。
Outer Apply详细说明:SQL Server中CROSS APPLY和OUTER APPLY应用
select a.*,b.*
from 表A as a
outer apply
(select top 1 * from 表B where a.id=b.id order by 排序字段 desc) as b
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。