从记录中获取最大价值

时间:2017-10-24 06:36:47

标签: sql sql-server

以下查询计算员工在一家商店工作的天数:

{{1}}

输出:

{{1}}

我希望得到他为每位员工工作更多天的商店,所以期望的输出应该是:

{{1}}

1 个答案:

答案 0 :(得分:4)

SELECT TOP 1 WITH TIES
  Outlet.Code,
 COUNT(DISTINCT (LogDate)) AS [Worked Days],
  AccessLog.EmployeeID AS [Employee ID],
  abr AS Outlet,
  GEmp.Name,
  Outlet.Brands
FROM [dbo].[AccessLog]
INNER JOIN dbo.Outlet
  ON dbo.Outlet.Code = dbo.AccessLog.TerminalID
INNER JOIN dbo.GEmp
  ON dbo.GEmp.EmpCode = dbo.AccessLog.EmployeeID
WHERE CONVERT(datetime, [LogDate]) BETWEEN '2017/10/01' AND '2017/10/31' and dbo.GEmp.EmpCode='362573'
AND Outlet.Brands = 'brand1'
AND [dbo].[AccessLog].InOut = '0'
GROUP BY AccessLog.EmployeeID,
         dbo.Outlet.abr,
         GEmp.Name,
         dbo.Outlet.Brands,
         dbo.Outlet.Malls,
         Outlet.Code
ORDER BY ROW_COUNT() OVER (PARTITION BY Outlet.Code ORDER BY [Worked Days] DESC)