月に分類された 53 週間のリストを作成する必要があります (これらは小売月であるため、小売業の担当者にとってはより意味のあるものになります)。
4-5-4 の間隔で最大 53 週間繰り返すリストが必要です。
たとえば、SQLフィドルを参照してください
これをテーブルではなく SQL ステートメントで記述したいと考えています。また、多くのユニオンも使用していません。rownum
andを使用する方法があると確信してconnect-by
いますが、ロジックが思いつきません!
これは私の試みです:
SELECT DISTINCT to_char(l_date,'WW') week_id ,
to_char(l_date, 'mm') month_id
FROM
(SELECT to_date('01/jan/2013', 'dd/mm/yyyy')+LEVEL-1 l_date
FROM dual
CONNECT BY LEVEL <= 365
ORDER BY 1
)
ORDER BY WEEK_ID;
SQL FIDDLE テーブルに従って編集:
SELECT week_id, MAX(month_id)
FROM
(SELECT DISTINCT TO_CHAR(l_date,'WW') week_id ,
TO_CHAR(l_date, 'mm') month_id
FROM
(SELECT TRUNC(SYSDATE, 'year')+LEVEL-1 l_date
FROM dual
CONNECT BY LEVEL <= 365
)
)
GROUP BY week_id
ORDER BY WEEK_ID;
私は以下を使用して終了しました。理想的ではありませんが、機能します:
SELECT ROWNUM AS WEEK_ID, 1 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 4
UNION ALL
SELECT ROWNUM+4 AS WEEK_ID, 2 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 5
UNION ALL
SELECT ROWNUM+9 AS WEEK_ID, 3 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 4
UNION ALL
SELECT ROWNUM+13 AS WEEK_ID, 4 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 4
UNION ALL
SELECT ROWNUM+17 AS WEEK_ID, 5 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 5
UNION ALL
SELECT ROWNUM+22 AS WEEK_ID, 6 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 4
UNION ALL
SELECT ROWNUM+26 AS WEEK_ID, 7 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 4
UNION ALL
SELECT ROWNUM+30 AS WEEK_ID, 8 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 5
UNION ALL
SELECT ROWNUM+35 AS WEEK_ID, 9 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 4
UNION ALL
SELECT ROWNUM+39 AS WEEK_ID, 10 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 4
UNION ALL
SELECT ROWNUM+43 AS WEEK_ID, 11 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 5
UNION ALL
SELECT ROWNUM+48 AS WEEK_ID, 12 AS MONTH_ID
FROM DUAL
CONNECT BY ROWNUM <= 5