为什么此分层查询不起作用?

时间:2015-11-25 01:45:17

标签: sql oracle join hierarchy

假设:

| ID_Composite1 | ID_Composite2 | DateStart | DateEnd   |
| 1             | 2             | 11-24-2015| 03-11-2016|
| 2             | 4             | 8-11-2015 | 12-11-2015|

我需要得到:

| ID_Composite1 | ID_Composite2 | Month     |
| 1             | 2             | 11-24-2015|
| 1             | 2             | 12-24-2015|
| 1             | 2             |  1-24-2016|
| 1             | 2             |  2-24-2016|
| 1             | 2             |  3-11-2016|
| 2             | 4             | 8-11-2015 |
| 2             | 4             | 9-11-2015 |
| 2             | 4             | 9-11-2015 |

我尝试了最简化的情况(使用关卡和连接方式生成一个范围仅在几个月之间)但我无法使其工作:

SQLFiddle

1 个答案:

答案 0 :(得分:0)

所以,我强制你的SQL上有一个迭代器,以确保有足够的行来使事情正常工作....

检查出来:

    SELECT firstcompositekey, secondcompositekey, to_char(add_months(startmonth, iterator),'MM-YYYY') as month
 FROM test, 
   (SELECT LEVEL iterator FROM dual CONNECT BY LEVEL <= 1000) iterations
WHERE add_months(startmonth, iterator) <= endmonth
ORDER BY 1,3

以下是SQLFiddle

希望这有帮助

相关问题