我可能會先將所有內容複製到臨時表中,然後移動不滿足end_date = start_date +1
的記錄,然後運行其餘的記錄,更新它們以便所有序列的start_date和end_date都在同一記錄中
因此,像這樣:
Temp_table_1
tableID User Start_Date End_Date
------- ---- ---------- ----------
1 111 2015-09-09 2015-09-15
2 111 2015-09-01 2015-09-02
3 111 2015-09-16 2015-09-21
4 111 2015-08-16 2015-08-21
5 111 2015-07-16 2015-07-21
移動非順序記錄temp_2
Temp_table_1
tableID User Start_Date End_Date
------- ---- ---------- ----------
1 111 2015-09-09 2015-09-15
3 111 2015-09-16 2015-09-21
Temp_table_2
tableID User Start_Date End_Date
------- ---- ---------- ----------
2 111 2015-09-01 2015-09-02
4 111 2015-08-16 2015-08-21
5 111 2015-07-16 2015-07-21
鞏固記錄
Temp_table_1
tableID User Start_Date End_Date
------- ---- ---------- ----------
x 111 2015-09-09 2015-09-21
移動合併記錄溫度2,計算差異
Temp_table_2
tableID User Start_Date End_Date Diff
------- ---- ---------- ---------- ------
2 111 2015-09-01 2015-09-02 2
4 111 2015-08-16 2015-08-21 5
5 111 2015-07-16 2015-07-21 5
x 111 2015-09-09 2015-09-21 11
看一看,嘗試寫一個程序,並更新你的問題,要求更如果你卡住了,請幫忙。
添加你試過的任何東西 –
你的問題不清楚 – Wanderer
如果我們有記錄像starte_date 2015-09-22和end_date 2015-09-30怎麼辦? –