SELECT CASE
WHEN (new_yweek =
MIN (new_yweek) OVER (partition by mon order by new_yweek)
)
THEN mon_name
ELSE NULL
End as month,
New_yweek AS yweek,
ROW_NUMBER () OVER (partition by mon order by new_yweek) AS mweek,
SUM (DECODE (wday, '1', mday, NULL) AS sun,
SUM (DECODE (wday, '2', mday, NULL) AS mon,
SUM (DECODE (wday, '3', mday, NULL) AS tue,
SUM (DECODE (wday, '4', mday, NULL) AS wed,
SUM (DECODE (wday, '5', mday, NULL) AS thu,
SUM (DECODE (wday, '6', mday, NULL) AS fri,
SUM (DECODE (wday, '7', mday, NULL) AS sat
FROM (SELECT dayofyear AS everyday, TO_CHAR (dayofyear, 'mm') AS mon,
TO_CHAR (dayofyear, 'month') AS mon_name,
TO_CHAR (dayofyear, 'w') AS mweek,
TO_CHAR (dayofyear, 'ww ') AS yweek,
CASE
WHEN (TO_CHAR (TO_DATE (& year | '201312', 'yyyymmdd'), 'D')>
'1'
)
AND (TO_CHAR (dayofyear, 'D') <
TO_CHAR (TO_DATE (& year | '201312', 'yyyymmdd '),
'D'
)
)
THEN TO_CHAR (dayofyear, 'ww ') + 1, 'fm00 ')
ELSE TO_CHAR (dayofyear, 'ww ')
End as new_yweek,
TO_CHAR (dayofyear, 'D') AS wday,
TO_CHAR (dayofyear, 'dd') AS mday
FROM (SELECT TO_DATE (& year | '20140901 ',
'Yyyymmdd'
)
+ LEVEL
-1 AS dayofyear
FROM DUAL
Connect by level <=
TO_CHAR (TO_DATE (& year | '201312', 'yyyymmdd '),
'Ddd'
)))
Group by mon, mon_name, new_yweek;