sql server 按外键分组查询

SELECT ROW_NUMBER() over(partition by a.yngrbsh order by a.SFRQ desc ) RN,
 c.* FROM JB_NCZ_GW_SFJL A,JB_MB_ZHXX_DJ C  WHERE A.MBDJLSH=C.LSH AND  a.SFJY='2' and a.LSH 
not in(select SFLSH from JB_NCZ_GW_ZZD b)

在结果中根据yngrbsh进行分组,然后可以取出RN等于1的数据。

posted @ 2016-11-29 15:34  权杖攻城狮  阅读(414)  评论(0编辑  收藏  举报