首页 > 代码库 > SQL Server分组查询某最大值的整条数据(包含linq写法)

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() };

  

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