0

tblというテーブルの単一のMySQL日時列から開始列と終了列を選択したいと思います。助言がありますか?

私が始めたもの:

+---------------------+
|      date_time      |
+---------------------+
| 2012-03-05 10:15:52 |
| 2012-03-05 10:15:53 |
| 2012-03-05 10:15:54 |
+---------------------+  

希望する結果:

+---------------------+---------------------+
|     start_time      |      end_time       |
+---------------------+---------------------+
| 2012-03-05 10:15:52 | 2012-03-05 10:15:53 |
| 2012-03-05 10:15:53 | 2012-03-05 10:15:54 |
+---------------------+---------------------+
4

2 に答える 2

4
SELECT   cur.date_time AS start_time, MIN(nxt.date_time) AS end_time
FROM     my_table AS cur JOIN my_table AS nxt ON nxt.date_time > cur.date_time
GROUP BY cur.date_time

sqlfiddleで参照してください。

于 2012-10-14T21:55:09.013 に答える
0
select end_time.date_time as [start], 
    (select 
    min(date_time) -- select smallest value from

    from da start_time --another table instance which i called start_time

        -- taking datetimes earlier than start 
    where start_time.date_time > end_time.date_time

    ) as [end]

from da end_time -- table instance with start time

--dont take last value (end time for that value will be null)
where 
    end_time.date_time < (select max(date_time) from da)    

私のテーブル:

TABLE [da]
Id  date_time
1   2012-03-05 10:15:52.000
2   2012-03-05 10:15:53.000
3   2012-03-05 10:15:54.000

私の結果:

start                   end
2012-03-05 10:15:52.000 2012-03-05 10:15:53.000
2012-03-05 10:15:53.000 2012-03-05 10:15:54.000
于 2012-10-14T22:05:43.500 に答える