其他分享
首页 > 其他分享> > 通过PARTITION BY 实现数据的分组

通过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;

 

标签:Cust,dbo,AddDate,PARTITION,HouseRent,LH,分组,数据,id
来源: https://www.cnblogs.com/Learnall/p/14500412.html