SQL Server grouping plus sequence number, only take the first few of the specified conditions of an object

 --  -- 删除base里冗余的数据
  --UPDATE dbo.N_Order_ServiceLog SET IsDel = 1 WHERE OrderId IN (
  SELECT OrderId FROM(
          SELECT ROW_NUMBER() OVER(PARTITION BY bs.Phone,sl.Companyid ORDER BY  sl.id desc) AS cindex,
           bs.Phone,sl.* FROM dbo.TableXXXXX   bs
 LEFT JOIN TableLLLLL  sl
 ON sl.OrderId = bs.OrderId
 WHERE bs.OrderType = 12 AND bs.CreateTime>'2018-04-25 10:29:00'
 ) a WHERE a.cindex>1
 )

 

Mark the serial number according to the store group

          SELECT ROW_NUMBER() OVER(PARTITION BY bs.Phone,sl.Companyid ORDER BY  sl.id desc) AS cindex,
           bs.Phone,sl.* FROM TableXXXXX bs
 LEFT JOIN TableLLLLL sl
 ON sl.OrderId = bs.OrderId
 WHERE bs.OrderType = 12 AND bs.CreateTime>'2018-04-25 10:29:00'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970030&siteId=291194637