簡體   English   中英

在Sql查詢中透視時間日志

[英]Pivoting Timelogs in Sql Query

我有點困在查詢我的timelogs表,以下是示例數據:

TimelogId     EmployeeId     RecordDate     RecordTime     Type
---------     ----------     ----------     ----------     ----
   1              4          2016-07-01     07:18:37         1
   2              4          2016-07-01     12:03:14         2
   5              4          2016-07-01     12:08:02         1
   6              4          2016-07-01     18:02:03         2
   7              6          2016-07-19     07:24:15         1
   8              5          2016-07-19     07:26:03         1
   9              6          2016-07-19     12:15:26         2
   10             5          2016-07-19     12:35:17         2
   13             5          2016-07-19     12:36:14         1
   16             6          2016-07-19     12:45:45         1
   17             6          2016-07-19     17:10:22         2
   18             5          2016-07-19     18:43:09         2

所需的輸出:

   Date      EmployeeId    Time_In      Time_Out      Time_In      Time_Out
  -------    ----------    --------     --------      -------      -------
2016-07-01       4         07:18:37     12:03:14      12:08:03    18:02:03
2016-07-19       6         07:24:15     12:15:26      12:45:45    17:10:22
2016-07-19       5         07:26:03     12:35:17      12:36:14    18:43:08

其中Type (1)= time in和Type (2)= time out。

員工需要在12nn注銷,然后在幾分鍾后重新登錄。

我嘗試使用基於我在此閱讀的先前問題的數據透視表。 雖然這是我第一次嘗試在表格中使用數據透視表。

select 
*
FROM
(
  select 
  EmployeeID,
  RecordDate,
  RecordTime,
  Type
  from tblTimeLogs
) d
PIVOT(
  Max(RecordTime) <---- I think the problem is right around here
  FOR Type in ([1],[2]) <----- plus i can't seem to rename this column names maybe i'll read a little further on this.
) piv;                        

輸出查詢:

EmployeeID          RecordDate          1                2
----------          ----------        ------          ------
    4               2016-07-01       12:08:02        18:02:03
    5               2016-07-19       12:36:14        18:43:09
    6               2016-07-19       12:45:45        17:10:22

這可能嗎? 謝謝。 :d

編輯:

正如vercelli所建議的那樣,另一種情況就是例如用戶忘了他/她已經在幾分鍾前定時,所以她再次進入。 例如

TimelogId     EmployeeId     RecordDate     RecordTime     Type
---------     ----------     ----------     ----------     ----
   1              4          2016-07-01     07:18:37         1
   2              4          2016-07-01     12:03:14         2
   5              4          2016-07-01     12:08:02         1
   6              4          2016-07-01     18:02:03         2
   7              6          2016-07-19     07:24:15         1
   8              5          2016-07-19     07:26:03         1
   9              6          2016-07-19     12:15:26         2
   10             5          2016-07-19     12:35:17         2
   13             5          2016-07-19     12:36:14         1
   16             6          2016-07-19     12:45:45         1
   17             6          2016-07-19     17:10:22         2
   18             5          2016-07-19     18:43:09         2
   19             5          2016-07-20     08:13:35         1  <--- Time in
   20             5          2016-07-20     08:14:35         1  <--- Timed In again
   21             5          2016-07-20     12:15:12         2  <--- Time Out

我嘗試使用Vercelli先生的查詢:

select 
EmployeeID as Emp, RecordDate, [1] as Time_In1, [2] as Time_Out1, [3] as Time_In2, [4] as Time_out2
FROM
(
  select 
  EmployeeID,
  RecordDate,
  RecordTime,
  ROW_NUMBER () over (partition by EmployeeId, RecordDate order by RecordTime, type) as rn
from tblTimeLogs
) d
PIVOT(
  max(RecordTime)
  FOR rn in ([1],[2],[3],[4])
) as piv;  

輸出新查詢:

 Emp   RecordDate    Time_In1    Time_Out1    Time_In2     Time_Out2
 ----  ----------    ---------   ---------    --------     ---------
  4    2016-07-01    07:18:37    12:03:14     12:08:02     18:02:03
  5    2016-07-19    07:26:03    12:35:17     12:36:14     18:43:09
  5    2016-07-20    08:13:35    08:14:35     12:15:12       Null <--- the problem is right around this portion
  6    2016-07-19    07:24:15    12:15:26     12:45:45     17:10:22

預期產出:

  Emp   RecordDate    Time_In1    Time_Out1    Time_In2    Time_Out2
 ----  ----------    ---------   ---------    --------     ---------
  4    2016-07-01    07:18:37    12:03:14     12:08:02     18:02:03
  5    2016-07-19    07:26:03    12:35:17     12:36:14     18:43:09
  5    2016-07-20    08:13:35    12:15:12     08:14:35       Null <--- the second time in would fall on the second 5th column (Time_In2) since the **Type** value is = 1      
  6    2016-07-19    07:24:15    12:15:26     12:45:45     17:10:22

謝謝你的幫助:D。 我正在從這個問題中學到新東西。

我想這就是你要找的東西。 演示

select 
EmployeeID as Emp, RecordDate, [1] as Time_In1, [2] as Time_Out1, [3] as Time_In2, [4] as Time_out2
FROM
(
  select 
  EmployeeID,
  RecordDate,
  RecordTime,
  ROW_NUMBER () over (partition by EmployeeId, RecordDate order by RecordTime, type) as rn
  from tblTimeLogs
) d
PIVOT(
  max(RecordTime)
  FOR rn in ([1],[2],[3],[4])
) as piv;  

OUPUT

Emp RecordDate  Time_In1    Time_Out1   Time_In2    Time_out2
4   2016-07-01  07:18:37    12:03:14    12:08:02    18:02:03
5   2016-07-19  07:26:03    12:35:17    12:36:14    18:43:09
6   2016-07-19  07:24:15    12:15:26    12:45:45    17:10:22

在這里演示

---表創建腳本

 create table #test
   (
   employeeid int,
   recorddate date,
   recordtime time,
   typee int
   )

insert into #test  values(    4 ,         '2016-07-01',     '07:18:37',         1)
insert into #test  values(    4 ,         '2016-07-01',     '12:03:14',         2)
insert into #test  values(    4 ,         '2016-07-01',     '12:08:02',         1)
insert into #test  values(    4 ,         '2016-07-01',     '18:02:03',         2)

詢問

 ;with cte
 as
 (
select
 employeeid,
 max(recorddate) as recorddate,
 min(recordtime) as timein,
max(recordtime) as Timein1 ,
 lead(min(recordtime)) over (partition by employeeid order by min(recordtime)) as 'timeout1',
 lead(max(recordtime)) over (partition by employeeid order by max(recordtime)) as 'timeout2'
 from #test
 group by 
 employeeid,typee
)
    select employeeid,recorddate,timein,timeout1,timein1,timeout2
    from cte
    where timeout1 is not null and timeout2 is not null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM