如何在Oracle中的一个变量上获取多个条目?

时间:2013-12-13 12:04:21

标签: oracle group-by oracle-sqldeveloper

我有这个:

var weekend weekstart
x   50_13   43_13

我需要这个:

 var week
   x  43
   x  44
   x  45
   x  46
   x  47
   x  48
   x  49
   x  50

出路是什么?

2 个答案:

答案 0 :(得分:1)

这个应该有效:

CREATE TABLE weeks ( var VARCHAR2(10), weekend VARCHAR2(5), weekstart VARCHAR2(5));
INSERT INTO weeks VALUES ('x', '50_13','43_13');

WITH 
startDay AS
    (SELECT var, MIN(TO_DATE('01-01-2000','dd-mm-yyyy') + LEVEL) AS start_date
    FROM weeks
    WHERE TO_CHAR(TO_DATE('01-01-2000','dd-mm-yyyy') + LEVEL, 'IW_RR') = weekstart
    CONNECT BY TO_DATE('01-01-2000','dd-mm-yyyy') + LEVEL < TO_DATE('01-01-2100','dd-mm-yyyy')
    GROUP BY var),
endDay AS
    (SELECT var, MIN(TO_DATE('01-01-2000','dd-mm-yyyy') + LEVEL) AS end_date
    FROM weeks
    WHERE TO_CHAR(TO_DATE('01-01-2000','dd-mm-yyyy') + LEVEL, 'IW_RR') = weekend
    CONNECT BY TO_DATE('01-01-2000','dd-mm-yyyy') + LEVEL < TO_DATE('01-01-2100','dd-mm-yyyy')
    GROUP BY var)
SELECT var, TO_CHAR(start_date + 7*(LEVEL-1), 'IW') AS week
FROM startDay
    JOIN endDay USING (var)
CONNECT BY start_date + 7*(LEVEL-1) <= end_date;

VAR WEEK
x   43
x   44
x   45
x   46
x   47
x   48
x   49
x   50

前提条件是,初始日期(01-01-2000)必须小于表格中可能出现的最小数字,并且最大日期(01-01-2100)大于可能出现的最大数字。

答案 1 :(得分:0)

这有点解决了我的问题:

select x, weekstart + level - 1
from 
myTable
connect by level <= weekend - weekstart + 1