Sql server根据时间最新的去除重复显示到前面

话不多说看效果:这原来的数据是有重复的,除了Id和创建时间不太相同以外,其他都是一模一样的。(什么业务需求不重要,重要的是怎么去实现需求)

sql:

select * from (select *,  ROW_NUMBER() OVER(ORDER BY CreateTime DESC) AS RowId from ( 
 SELECT
 a.Id,
a.D_Id
,a.Price
,a.StartTime
,a.EndTime
,a.CreateTime
,ISNULL(b.Jiancheng, b.Dname) as JianCheng
,c.Name as PdcName
,a.GrossWeight
 FROM [tb_Contract] a
 JOIN  tb_Dept b on a.D_Id =b.Id
LEFT JOIN  tb_Products c on b.PdcId =c.Id WHERE  1=1 
) t) page where RowId between 1 and 80

 

 

 

 最终想要得到的:

sql:

select * from (select *,  ROW_NUMBER() OVER(ORDER BY Row asc,CreateTime desc ) AS RowId from ( 
SELECT  
a.Id
,a.D_Id
,a.Price
,a.StartTime
,a.EndTime
,a.CreateTime
,ISNULL(b.Jiancheng, b.Dname) as JianCheng
,c.Name as PdcName
,a.GrossWeight
,a.Row
 FROM
    (select a.*,row_number() over (partition by D_Id order by CreateTime desc) Row
 
    from tb_Contract a
) a
LEFT JOIN  tb_Dept b on a.D_Id =b.Id
LEFT JOIN  tb_Products c on b.PdcId =c.Id WHERE  1=1  

) t) page where RowId between 1 and 80

 

 

posted @ 2022-10-19 15:39  Valoris  阅读(301)  评论(0编辑  收藏  举报