2016-07-18 2 views
1

내 timelogs 테이블을 쿼리에 가지 붙어 오전, 다음과 같은 샘플 데이터입니다피벗 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) = 시간은 Type (2) = 시간 초과입니다.

직원은 12nn에서 로그 아웃 한 다음 2 분 후에 다시 로그인해야합니다.

여기에서 읽은 이전 질문에 기반한 피벗을 사용해 보았습니다. 이것은 테이블에서 피벗을 사용하려고 처음이지만. 쿼리의

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

편집 : 베르 첼리에 의해 제안

이 같은 또 다른 시나리오는 예를 들어 사용자가 전에 그래서 그녀는 다시 시간이 초과 그/그녀가 이미 단 몇 분의 시간이 초과 것을 잊어 버렸습니다. 예 : 새 쿼리의

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; 

출력 :

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 

은 씨 베르 첼리의 쿼리를 사용하여 시도

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. 나는이 문제로부터 새로운 것을 배우고있다.

답변

0

나는 이것이 당신이 찾고있는 것이라고 생각합니다. Demo :

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 
+0

답변 해 주셔서 감사합니다. 쿼리가 포함하는'type' 값에 따라 결과 나 행을 필터링합니까? : D 나는 여기에 예제를 만들려고 시도했다. [link] (https://data.stackexchange.com/meta.stackexchange/query/514106/pivoting-timelogs-in-sql-query2). 예를 들어, 사용자가 잠시 전에 로그인 한 것을 잊어 버렸기 때문에 두 번 로그인 한 사용자와 같습니다. – jkw

+0

@jkw 무슨 뜻인지 알지만 어떻게 보여줄 수 있습니까? 이 새 시나리오를 표시하기 위해 질문을 편집하십시오 – vercelli

+0

질문을 업데이트했습니다. 다시 한 번 감사드립니다. D. – jkw

0

DEMO HERE

--- 표 Creattion 스크립트

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 
+0

timeout> timein1, 다른 방법이어야 함 – vercelli

+0

@vercelli : 업데이트 된 체크 – TheGameiswar

+0

@TheGameiswar : 응답 해 주셔서 감사합니다. 쿼리를 시도했지만 출력이 [Err] 42000 - [SQL Server] 'lead '은 (는) 인식 된 내장 함수 이름이 아닙니다. 아마도 비 호환성 문제로 인해. 필자는 Microsoft 사이트에서 기능이 내장되어 있음을 읽었습니다. 2012 년 이후 버전부터 시작합니다. – jkw