2016-12-14 64 views
1

我有數據如下MS SQL 2012(要轉數據)

enter image description here

,並希望輸出

Name  Check In Date Check Out Date Check In Time Check Out Time 
Employee 1 20/11/2016  21/11/2016  03:52:15 PM  12:08:50 AM 
Employee 1 21/11/2016  22/11/2016  03:45:37 PM  12:14:25 AM 
Employee 1 22/11/2016  23/11/2016  03:41:34 PM  12:04:53 AM 

我試着當條件使用情況,但沒有得到正確輸出。 請幫助

問候 邁克爾

+1

每天只有一次入住/退房? –

回答

-1

您問題的標題說:「MS SQL 2012」,但您使用了「SQL服務器2008」的標籤。這個解決方案將在兩個工作。如果您完全按照您的要求顯示,您仍然需要格式化日期和時間。

IF OBJECT_ID('tempdb..#Test', 'U') IS NOT NULL DROP TABLE #Test 

SELECT 'Employee 1' AS Name, 
'Check In' AS Type, 
CONVERT(DATETIME, '11/20/2016 3:52:15 PM') AS DateTime 
INTO #Test 
UNION 
SELECT 'Employee 1' AS Name, 
'Check Out' AS Type, 
CONVERT(DATETIME, '11/21/2016 12:08:50 AM') AS DateTime 
UNION 
SELECT 'Employee 1' AS Name, 
'Check In' AS Type, 
CONVERT(DATETIME, '11/21/2016 03:45:37 PM') AS DateTime 
UNION 
SELECT 'Employee 1' AS Name, 
'Check Out' AS Type, 
CONVERT(DATETIME, '11/22/2016 12:14:25 AM') AS DateTime 
UNION 
SELECT 'Employee 1' AS Name, 
'Check In' AS Type, 
CONVERT(DATETIME, '11/22/2016 03:41:34 PM') AS DateTime 
UNION 
SELECT 'Employee 1' AS Name, 
'Check Out' AS Type, 
CONVERT(DATETIME, '11/23/2016 12:04:53 AM') AS DateTime 
UNION 
SELECT 'Employee 1' AS Name, 
'Check In' AS Type, 
CONVERT(DATETIME, '11/23/2016 03:41:34 PM') AS DateTime 

; 
WITH CTE AS (
SELECT rownum = ROW_NUMBER() OVER (ORDER BY DateTime), 
Name, Type, DateTime 
FROM #Test 
) 
SELECT cte.Name, 
CONVERT(DATE, cte.DateTime) AS CheckInDate, 
CONVERT(DATE, nxt.DateTime) AS CheckOutDate, 
CONVERT(TIME, cte.DateTime) AS CheckInTime, 
CONVERT(TIME, nxt.DateTime) AS CheckOutTime 
FROM CTE 
LEFT JOIN CTE nxt ON nxt.rownum = CTE.rownum + 1 
WHERE cte.Type = 'Check In' 

如果您使用的是SQL Server 2012中您可以使用LAGLEAD功能。這種方法的基礎和解釋可以在here找到。

+0

我很好奇投票的理由。我的答案產生了要求將基本格式設置留給OP的內容。 – nscheaffer

+0

謝謝....兩種方式工作 – Michael

+0

問候....我現在需要幫助。 – Michael

1

這將通過使用窗口功能得到你想要的。

if object_id('tempdb..#tt') is not null drop table #tt 

create table #tt (name varchar(45), [status] varchar(16), [Date] varchar(16), [Time] varchar(16)) 

insert into #tt (name, [status], [date], [time]) values 

('Employee 1','Check In','20/11/2016','03:52:15 PM'), 
('Employee 1','Check Out','21/11/2016','12:08:50 AM'), 
('Employee 1','Check In','21/11/2016','03:45:37 PM'), 
('Employee 1','Check Out','22/11/2016','12:14:25 PM'), 
('Employee 1','Check In','22/11/2016','03:41:34 PM'), 
('Employee 1','Check Out','23/11/2016','12:04:53 PM') 


;with cte as(
    select 
     *, 
     ROW_NUMBER() over (partition by [Name] order by convert(datetime,[Date] + ' ' + convert(varchar(8),cast([time] as time)),103)) as rownum 
     from #tt) 


select 
    ci.Name, 
    ci.[Date] as CheckInDate, 
    ci.[Time] as CheckInTime, 
    co.[Date] as CheckOutDate, 
    co.[Time] as CheckOutTime 
from 
    cte ci 
    inner join cte co on 
    co.rownum = ci.rownum + 1 
where ci.status = 'Check In' 


--Results 
Name  CheckInDate CheckInTime CheckOutDate CheckOutTime 
Employee 1 20/11/2016 03:52:15 PM 21/11/2016 12:08:50 AM 
Employee 1 21/11/2016 03:45:37 PM 22/11/2016 12:14:25 PM 
Employee 1 22/11/2016 03:41:34 PM 23/11/2016 12:04:53 PM