zoukankan      html  css  js  c++  java
  • SQL取某分组(或类别)中数值最大的一个

                WITH T1 AS
                (SELECT 
                EmployeeArchive.Person,
                RouteHIstory.Route RouteId,
                COUNT(OperatingData.OperatingDate) AS DayCount
                FROM BUS_TradingkmDoc TradingkmDoc
                    LEFT JOIN BUS_OperatingData OperatingData ON TradingkmDoc.OperatingDataDoc = OperatingData.ID
                    LEFT JOIN BUS_RouteHIstory RouteHIstory ON RouteHIstory.ID = OperatingData.RouteHistory
                    LEFT JOIN CBO_EmployeeArchive EmployeeArchive ON EmployeeArchive.ID = TradingkmDoc.Pilot
                WHERE 
                OperatingData.OperatingDate BETWEEN '2021-03-01' AND '2021-03-31'           
                AND OperatingData.Org=1001309103393906 AND EmployeeArchive.Person IS NOT NULL
                GROUP BY Person,RouteHIstory.Route
                )
                SELECT T2.Person,T2.RouteId,R.Name AS RouteName,R.StarGrade,DBO.F_GetEnumName('UFIDA.U9.BUS.RouteBE.StarGradeEnum',R.StarGrade,'zh-cn') AS 线路星级 FROM (
                   select *,ROW_NUMBER() OVER(PARTITION BY Person ORDER BY DayCount DESC) AS Num FROM T1
                )T2 LEFT JOIN BUS_Route R ON T2.RouteId=R.ID
                WHERE T2.Num=1 order by Person

    主要代码:

    select *,ROW_NUMBER() OVER(PARTITION BY Person ORDER BY DayCount DESC) AS Num FROM T1;  Person字段 DayCount最大的一个;
  • 相关阅读:
    [NOI2005]维修数列
    [CTSC2008]网络管理Network
    Directory类
    System.Uri类
    ViewBag、ViewData和TempData使用方法、区别与联系
    Asp.net MVC 视图之公用代码
    MVC3 Razor模板引擎
    MVC之ActionResult
    Html辅助方法 之 Form表单标签
    MVC之 自定义过滤器(Filter)
  • 原文地址:https://www.cnblogs.com/fjzhang/p/14943396.html
Copyright © 2011-2022 走看看