提取对应于MIN(Column)和MAX(Column)的位置列

时间:2018-08-06 12:40:01

标签: sql sql-server stored-procedures

repeated

我如何获取与packed对应的 Select City, Name, MarkDate,Location MIN(InTime) as InTime ,MAX(OutTime)as OutTime,SUM(Distance) as Distance,SUM(Duration) as Duration from tblAttendanceTracker JOIN tblEmployee ON tblAttendanceTracker.EId= tblEmployee.EId Group By MarkDate,City ,Name Order By MarkDate ,City,Name asc 列和与Location对应的位置。因为位置是单个列

1 个答案:

答案 0 :(得分:1)

通常,您提供的SQL根本无法工作并给出错误。我收到了您的问题,因为您想根据出入日期时间找到每个员工和城市的位置:

WITH summary
AS (SELECT Eid,
           City,
           MarkDate,
           MIN(InTime) AS InTime,
           MAX(OutTime) AS OutTime,
           SUM(Distance) AS Distance,
           SUM(Duration) AS Duration
    FROM tblAttendanceTracker
    GROUP BY Eid,
             MarkDate,
             City)
SELECT s.City,
       e.Name,
       s.MarkDate,
       t.Location,
       s.InTime,
       s.OutTime,
       s.Distance,
       s.Duration
FROM summary s
    INNER JOIN tblAttendanceTracker t
        ON s.Eid = t.Eid
           AND s.City = t.City
           AND s.Markdate = t.Markdate
           AND
           (
               s.InTime = t.InTime
               OR s.OutTime = t.OutTime
           )
    INNER JOIN tblEmployee e
        ON s.EId = e.EId
ORDER BY s.MarkDate,
         s.City,
         e.Name ASC;