假設我有一個數據集:SQL - 如何根據現有數據集中的日期範圍爲每個月生成行?
rowID | dateStart | dateEnd | Year | Month
121 | 2013-10-03 | 2013-12-03 | NULL | NULL
143 | 2013-12-11 | 2014-03-11 | NULL | NULL
322 | 2014-01-02 | 2014-02-11 | NULL | NULL
而且我想SQL生成基於該dateStart和dateEnd以下數據源。請注意年份和月份分組。
rowID | dateStart | dateEnd | Year | Month
121 | 2013-10-03 | 2013-12-03 | 2013 | 10
121 | 2013-10-03 | 2013-12-03 | 2013 | 11
121 | 2013-10-03 | 2013-12-03 | 2013 | 12
143 | 2013-12-11 | 2014-03-11 | 2013 | 12
143 | 2013-12-11 | 2014-03-11 | 2014 | 1
143 | 2013-12-11 | 2014-03-11 | 2014 | 2
143 | 2013-12-11 | 2014-03-11 | 2014 | 3
322 | 2014-01-02 | 2014-02-11 | 2014 | 1
322 | 2014-01-02 | 2014-02-11 | 2014 | 2
我很難把這一個包裹起來。有任何想法嗎?
全部3條回覆!我選擇了Gordon's,因爲它允許我做一個外連接,並忽略具有額外過濾參數的行(我沒有要求這個,但我後來可以實現)。 JBrooks解決方案非常棒,因爲它的佔用空間更小,並且允許我利用函數,而shree的解決方案對於我的大型項目來說有點過於冗長,但仍然完全符合我的需求。謝謝你們! – rodmunera