SQL连接本身获得第一行,没有重复

时间:2011-07-06 21:47:39

标签: sql join

您好试着在SQL

中获得自我加入的清晰结果

这是SQL:

SELECT DISTINCT(a.EVENTDATE)'Date',
                a.PERSONFULLNAME'Employee',
                a.STARTDTM'ShiftStart', 
                a.OUTPUNCHDTM'BreakStart',
                b.STARTDTM 'BreakEnd',
                b.OUTPUNCHDTM 'ShiftEnd' 
FROM vp_timesheetpunch a 
LEFT OUTER JOIN vp_timesheetpunch b ON 
                a.EVENTDATE=b.EVENTDATE AND 
                a.STARTDTM <> b.STARTDTM AND 
                a.PERSONNUM=b.PERSONNUM AND 
                a.STARTDTM < b.STARTDTM 
WHERE a.PERSONNUM='104739' 
GROUP BY a.EVENTDATE, 
         a.PERSONFULLNAME, 
         a.STARTDTM, 
         b.STARTDTM, 
         a.OUTPUNCHDTM, 
         b.OUTPUNCHDTM
ORDER BY a.EVENTDATE

结果(记录休息日的2条记录*):

2011-06-24  LAST, F  2011-06-24 07:30  2011-06-24 14:00  2011-06-24 15:00  2011-06-24 20:36 
2011-06-24 00:00  LAST, F  2011-06-24 15:00  2011-06-24 20:36  NULL  NULL

*我需要在任何一天抑制第二个结果

2 个答案:

答案 0 :(得分:0)

@ X-Zero有一个观点 - 查询返回BreakStart和BreakEnd两列,所以如果一个员工休息两次,就没有办法显示有意义的结果。

假设员工每天休息0或1次,这可能会给你你所要求的:

-- This part gets data for everyone that took a break.
-- Hopefully they took only ONE break
-- (if they took more, we'll get multiple rows here)
Select A.EventDate      'Date'
      ,A.PersonFullName 'Employee'
      ,A.StartDtm       'ShiftStart'
      ,A.OutPunchDtm    'BreakStart'
      ,B.StartDtm       'BreakEnd'
      ,B.OutPunchDtm    'ShiftEnd'
  From vp_TimeSheetPunch A
  Join vp_TimeSheetPunch B On A.EventDate=B.EventDate
                         And A.PersonNum=B.PersonNum
                         And A.StartDtm<B.StartDtm
Union
-- This part gets data for everyone that did not take a break.
Select C.EventDate      'Date'
      ,C.PersonFullName 'Employee'
      ,C.StartDtm       'ShiftStart'
      ,Null             'BreakStart'
      ,Null             'BreakEnd'
      ,C.OutPunchDtm    'ShiftEnd'
  From vp_TimeSheetPunch C
 Where Not Exists(Select *
                    From vp_timesheetpunch D
                   Where C.EventDate=D.EventDate
                     And C.PersonNum=D.PersonNum
                     And C.StartDtm<>D.StartDtm)

-- This orders the result using column numbers
Order By 1,2,3,4

这应该这样做。

答案 1 :(得分:0)

如果每位员工和日期最多只有两行:

SELECT a.EVENTDATE      'Date',
       a.PERSONFULLNAME 'Employee',
       a.STARTDTM       'ShiftStart', 
       a.OUTPUNCHDTM    'BreakStart',
       b.STARTDTM       'BreakEnd',
       b.OUTPUNCHDTM    'ShiftEnd' 
FROM 
    ( SELECT EVENTDATE,
             PERSONNUM,       
             PERSONFULLNAME, 
             MIN(STARTDTM)    AS STARTDTM,       
             MIN(OUTPUNCHDTM) AS OUTPUNCHDTM  
      FROM vp_timesheetpunch
      WHERE PERSONNUM = '104739' 
      GROUP BY EVENTDATE, 
               PERSONNUM, 
               PERSONFULLNAME
    ) AS a
LEFT OUTER JOIN vp_timesheetpunch b ON 
                a.EVENTDATE = b.EVENTDATE AND 
                a.PERSONNUM = b.PERSONNUM AND 
                a.STARTDTM  < b.STARTDTM 
ORDER BY a.EVENTDATE