0
RCDID, EmployeeID, LogDate, LogTime, TerminalID, InOut, read

3079184, 'A00075', '2009/10/28', '07:17:10  ', 'VC01 ', 'IN        ', '1'
3079185, 'A00075', '2009/10/28', '17:28:51  ', 'VC01 ', 'OUT       ', '1'
3079186, 'A00038', '2009/10/28', '07:29:17  ', 'VC01 ', 'IN        ', '1'
3079187, 'A00038', '2009/10/28', '17:30:05  ', 'VC01 ', 'OUT       ', '1'
3079188, 'A00085', '2009/10/28', '07:37:34  ', 'VC01 ', 'IN        ', '1'
3079189, 'A00085', '2009/10/28', '17:43:14  ', 'VC01 ', 'IN        ', '1'

こんにちは、上記は私のソース テーブル (mysql) で、次のようにデータをクロス集計したいと考えています。

EmployeeID, LogDate, In_location, in_time, Out_location, Out_time
'A00001', '2009/10/28', 'VC01', '08:37:55  ', '', '', 
'A00001', '2009/10/29', 'VC01', '08:09:57  ', 'VC01            ', '17:09:32  '
'A00001', '2009/10/30', 'VC01 ', '09:48:41  ', 'VC01            ', '20:40:37  '
'A00001', '2009/11/03', 'VC01', '08:20:34  ', 'VC01            ', '18:03:34  '
'A00001', '2009/11/04', 'VC01 ', '08:26:49  ', 'VC01            ', '19:21:46  '
'A00001', '2009/11/05', 'VC01', '08:16:00  ', 'VC01            ', '19:26:01  '

誰か助けてください。私はあなたの助けに本当に感謝しています

4

1 に答える 1

1

基本的に、同じ日に同じ従業員によるIN次の動きとペアになるには、自己結合を実行する必要があります。OUT

SELECT   a.EmployeeID, a.LogDate,
         a.LogTime AS In_time,
         MIN(b.LogTime) AS Out_time
FROM     my_table a LEFT JOIN my_table b ON
         a.EmployeeID = b.EmployeeID
     AND a.LogDate    = b.LogDate
     AND a.LogTime    < b.LogTime
     AND b.InOut      = 'OUT'
WHERE    a.InOut      = 'IN'
GROUP BY EmployeeID, LogDate, In_time

INただし、対応するレコードがないレコードがある場合を除きOUTます。MySQL は をネイティブでサポートしていないためFULL OUTER JOIN、代わりに を使用して、同じ日付の同じ従業員による先行のUNION移動とペアになる同様のクエリと上記を組み合わせる必要があります。OUTIN

  SELECT   a.EmployeeID, a.LogDate,
           a.LogTime AS In_time,
           MIN(b.LogTime) AS Out_time
  FROM     my_table a LEFT JOIN my_table b ON
           a.EmployeeID = b.EmployeeID
       AND a.LogDate    = b.LogDate
       AND a.LogTime    < b.LogTime
       AND b.InOut      = 'OUT'
  WHERE    a.InOut      = 'IN'
  GROUP BY EmployeeID, LogDate, In_time

UNION

  SELECT   a.EmployeeID, a.LogDate,
           MAX(a.LogTime) AS In_time,
           b.LogTime AS Out_time
  FROM     my_table a RIGHT JOIN my_table b ON
           a.EmployeeID = b.EmployeeID
       AND a.LogDate    = b.LogDate
       AND a.LogTime    < b.LogTime
       AND a.InOut      = 'IN'
  WHERE    b.InOut      = 'OUT'
  GROUP BY EmployeeID, LogDate, Out_time

この詳細を取得したら、移動がログに記録された端末を抽出するために、結果をテーブルと再度結合する必要があります。

SELECT t.EmployeeID,
       t.LogDate,
       a.TerminalID AS In_location,
       t.In_time,
       b.TerminalID AS Out_location,
       t.Out_time
FROM (

  SELECT   a.EmployeeID, a.LogDate,
           a.LogTime AS In_time,
           MIN(b.LogTime) AS Out_time
  FROM     my_table a LEFT JOIN my_table b ON
           a.EmployeeID = b.EmployeeID
       AND a.LogDate    = b.LogDate
       AND a.LogTime    < b.LogTime
       AND b.InOut      = 'OUT'
  WHERE    a.InOut      = 'IN'
  GROUP BY EmployeeID, LogDate, In_time

UNION

  SELECT   a.EmployeeID, a.LogDate,
           MAX(a.LogTime) AS In_time,
           b.LogTime AS Out_time
  FROM     my_table a RIGHT JOIN my_table b ON
           a.EmployeeID = b.EmployeeID
       AND a.LogDate    = b.LogDate
       AND a.LogTime    < b.LogTime
       AND a.InOut      = 'IN'
  WHERE    b.InOut      = 'OUT'
  GROUP BY EmployeeID, LogDate, Out_time

) t

  LEFT JOIN my_table a ON
       a.EmployeeID = t.EmployeeID
   AND a.LogDate    = t.LogDate
   AND a.LogTime    = t.In_time
   AND a.InOut      = 'IN'

  LEFT JOIN my_table b ON
       b.EmployeeID = t.EmployeeID
   AND b.LogDate    = t.LogDate
   AND b.LogTime    = t.Out_time
   AND b.InOut      = 'OUT'

sqlfiddleで参照してください。

于 2012-08-29T08:21:48.643 に答える