2013-05-08 31 views
0

我有一個場景,我想根據日期和時間差異將我的日期列拆分爲SQL中的兩個日期列。例如將一個日期列拆分爲兩個

I have a date column with date and time. 

     Date 
2011-12-31 15:10:00 
2011-12-31 19:20:00 
2011-12-31 20:33:00 

Now i want to split it like. 

    From Date    To Date 
2011-12-31 15:10:00  2011-12-31 19:20:00 
2011-12-31 19:20:00  2011-12-31 20:33:00 

等等....

此外,如果有任何日期的差異,我想再次分裂e.g;

 From Date    To Date 
2011-12-31 15:10:00  2012-1-30 19:20:00 
2011-1-30 19:20:00  2012-2-28 20:33:00  

我希望我能理解。如果您需要更多說明,請讓我知道。

+1

這看起來像經典島嶼問題。其相對部分是缺口問題。在這裏閱讀更多信息:http://www.sqltopia.com/?page_id=83 您可以將記錄按兩個和兩個按照如何按某個值排序(在此情況下爲Date),然後使輸出成對。 – 2013-05-08 09:43:10

回答

4

這是你在找什麼?

WITH numbered AS(
    SELECT [Date], ROW_NUMBER()OVER(ORDER BY [Date]) rn 
    FROM dbo.YourTable 
) 
SELECT [From].Date AS [From Date], [To].Date AS [To Date] 
FROM numbered AS [From] 
JOIN numbered AS [To] 
ON [From].rn + 1 = [To].rn 
ORDER BY [From].Date; 
+0

我很感謝你的努力。謝謝!!!! – 2013-05-08 09:52:15

+0

我很滿意你的答案,這是非常有用的答案。 – 2013-05-08 09:58:46

+0

這應該是答案:)。 – 2013-05-11 08:51:52

1

嘗試這一個 -

查詢:

DECLARE @temp TABLE 
(
     col DATETIME 
) 

INSERT INTO @temp (col) 
VALUES 
    ('2011-12-31 15:10:00'), 
    ('2011-12-31 19:20:00'), 
    ('2011-12-31 20:33:00') 

SELECT * 
FROM @temp t 
OUTER APPLY (
    SELECT TOP 1 t2.col 
    FROM @temp t2 
    WHERE t2.col > t.col 
    ORDER BY t2.col 
) t2 
WHERE t2.col IS NOT NULL 

;WITH cte AS 
(
    SELECT col, ROW_NUMBER() OVER(ORDER BY col) rn 
    FROM @temp 
) 
SELECT f.col, t.col 
FROM cte f 
JOIN cte t ON f.rn + 1 = t.rn 

輸出:

col      col 
----------------------- ----------------------- 
2011-12-31 15:10:00.000 2011-12-31 19:20:00.000 
2011-12-31 19:20:00.000 2011-12-31 20:33:00.000 
+0

不客氣@ZeeShan。 – Devart 2013-05-08 09:49:26

+0

對不起,您的代碼在我的場景中對我來說效果不佳。但感謝您的幫助:)。 – 2013-05-11 08:54:10

1
You can try that using looping through the column , might not be perfect answer you look need to insert the result into a temp but this logic might work , the advantage is that you have have any other logic in to this code 

if object_id('tempdb..#Temp','u') is not null 
    Drop table #Temp 
    Create Table #Temp 
    (
    sno int identity(1,1), 
    datevalue datetime 
    ) 

    insert into #temp values ('2011-12-31 15:10:00'), 
      ('2011-12-31 19:20:00'), 
      ('2011-12-31 20:33:00') 
    Select * from #temp 
    DEclare @loop int=1, @column1 datetime,@column2 datetime 
    While (@loop<=(select max(sno) from #temp)) 
    Begin 
      Select @column1 =(select datevalue from #temp where [email protected]) ,@column2=(select datevalue from #temp where [email protected]+1) 
      Select @column1,@column2 
    set @[email protected]+1 

    End 

比ks, Arun

+0

我很感謝你的努力。謝謝Arun !!!! – 2013-05-08 09:52:52

1

如果我理解正確,您希望按記錄方式將記錄配對在一起。 這裏是一個SQL小提琴的問題:http://sqlfiddle.com/#!6/02afb/7

在cte的排名日期讓我們按順序配對記錄。

WITH rankedDates as 
(
SELECT 
    id_Date 
    ,ROW_NUMBER() over (order by id_Date) as rn 
    FROM test 
) 
SELECT 
startd.id_Date as startDateTime 
,endd.id_Date as endDateTime 
FROM rankedDates as startd 
INNER JOIN rankedDates as endd 
    ON startd.rn +1 = endd.rn 
+0

不錯的解決方案。謝謝David ... – 2013-05-08 09:56:29