通过PARTITION BY 实现数据的分组

需求:查询出每一个会员最晚发布的一条出租商铺信息

SELECT TOP 4
        LH.ID ,
        LH.Cust_id ,
        LH.Title ,
        LH.AddDate ,
        LH.Classification  --,LH.add_domain domain      
FROM    ( SELECT    LP_HouseRent.ID ,
                    Cust_id ,
                    Title ,
                    AddDate ,
                    Classification ,
                    ROW_NUMBER() OVER ( PARTITION BY Cust_id ORDER BY AddDate DESC ) new_id
          FROM      dbo.LP_HouseRent WITH ( NOLOCK )
                    INNER JOIN dbo.huiyuan ON huiyuan.id = LP_HouseRent.Cust_id
          WHERE     State >= 1
                    AND dbo.huiyuan.Verify > 2  -- AND add_vip =1 AND add_verify >2
        ) LH
WHERE   LH.new_id = 1
ORDER BY LH.AddDate DESC;

 

posted @ 2021-03-08 16:28  小白膜拜大佬  阅读(125)  评论(0编辑  收藏  举报