SQL Server分组查询某最大值的整条数据(包含linq写法)

时间:2021-05-05 15:13:25

想实现如下效果,就是分组后时间最大的那一条数据:

SQL Server分组查询某最大值的整条数据(包含linq写法)

 

1、SQL

        SELECT  *
        FROM    ( SELECT    * ,
                            ROW_NUMBER() OVER ( PARTITION BY RIP_GUID ORDER BY RU_CreatedTime DESC ) rn
                  FROM      RIP_FlowInfo
                ) t
        WHERE   t.rn <= 1;  

2、LINQ

    var groupQuery = from t in query group t by t.RIP_Guid into g select new { key = g.Key, source = g.OrderByDescending(tt => tt.RU_CreatedTime).FirstOrDefault() };