with x as(select
to_char(everyday,'yyyy-mm') as ym,
to_char(everyday,'yyyy') as year,
to_char(everyday,'mm') as month,
case
when to_char(everyday,'mm')='01' and to_char(everyday,'iw')='53' and to_char(everyday,'dy')!='星期日' then 0
when to_char(everyday,'mm')='01' and to_char(everyday,'iw')='53' and to_char(everyday,'dy')='星期日' and to_char(everyday,'dd')='01' then 0
when to_char(everyday,'mm')='01' and to_char(everyday,'iw')='53' and to_char(everyday,'dy')='星期日' then 1
when to_char(everyday,'mm')='01' and to_char(everyday,'iw')='52' then 1
when to_char(everyday,'mmdd')!='0101' and to_char(everyday,'dy')='星期日' then to_number(to_char(everyday,'iw'))+1
when to_char(everyday,'mm')='12' and to_char(everyday,'iw')='01' then 53
else
to_number(to_char(everyday,'iw'))
end as week,
lpad(decode(to_char(everyday,'dy'),'星期日',to_char(everyday,'dd')),3) as 星期日,
lpad(decode(to_char(everyday,'dy'),'星期一',to_char(everyday,'dd')),3) as 星期一,
lpad(decode(to_char(everyday,'dy'),'星期二',to_char(everyday,'dd')),3) as 星期二,
lpad(decode(to_char(everyday,'dy'),'星期三',to_char(everyday,'dd')),3) as 星期三,
lpad(decode(to_char(everyday,'dy'),'星期四',to_char(everyday,'dd')),3) as 星期四,
lpad(decode(to_char(everyday,'dy'),'星期五',to_char(everyday,'dd')),3) as 星期五,
lpad(decode(to_char(everyday,'dy'),'星期六',to_char(everyday,'dd')),3) as 星期六
from(select to_date('20160101','yyyymmdd') + level - 1 as everyDay from dual
connect by level <= last_day(to_date('20201201','yyyymmdd'))-to_date('20160101','yyyymmdd')+1
--(last_day(to_date('20061101','yyyymmdd')) - to_date('20061101','yyyymmdd') +1)
))
select case when max(星期日)=' 01' or max(星期一)=' 01' or max(星期二)=' 01' or max(星期三)=' 01' or max(星期四)=' 01' or max(星期五)=' 01' or max(星期六)=' 01' then year||'年'||month||'月' else '' end,max(星期日) SUN,max(星期一) MON,max(星期二) TUE,max(星期三) WED,max(星期四) THU,max(星期五) FRI,max(星期六) SAT from x group by year,month,week,ym order by year,month,week;
to_char(everyday,'yyyy-mm') as ym,
to_char(everyday,'yyyy') as year,
to_char(everyday,'mm') as month,
case
when to_char(everyday,'mm')='01' and to_char(everyday,'iw')='53' and to_char(everyday,'dy')!='星期日' then 0
when to_char(everyday,'mm')='01' and to_char(everyday,'iw')='53' and to_char(everyday,'dy')='星期日' and to_char(everyday,'dd')='01' then 0
when to_char(everyday,'mm')='01' and to_char(everyday,'iw')='53' and to_char(everyday,'dy')='星期日' then 1
when to_char(everyday,'mm')='01' and to_char(everyday,'iw')='52' then 1
when to_char(everyday,'mmdd')!='0101' and to_char(everyday,'dy')='星期日' then to_number(to_char(everyday,'iw'))+1
when to_char(everyday,'mm')='12' and to_char(everyday,'iw')='01' then 53
else
to_number(to_char(everyday,'iw'))
end as week,
lpad(decode(to_char(everyday,'dy'),'星期日',to_char(everyday,'dd')),3) as 星期日,
lpad(decode(to_char(everyday,'dy'),'星期一',to_char(everyday,'dd')),3) as 星期一,
lpad(decode(to_char(everyday,'dy'),'星期二',to_char(everyday,'dd')),3) as 星期二,
lpad(decode(to_char(everyday,'dy'),'星期三',to_char(everyday,'dd')),3) as 星期三,
lpad(decode(to_char(everyday,'dy'),'星期四',to_char(everyday,'dd')),3) as 星期四,
lpad(decode(to_char(everyday,'dy'),'星期五',to_char(everyday,'dd')),3) as 星期五,
lpad(decode(to_char(everyday,'dy'),'星期六',to_char(everyday,'dd')),3) as 星期六
from(select to_date('20160101','yyyymmdd') + level - 1 as everyDay from dual
connect by level <= last_day(to_date('20201201','yyyymmdd'))-to_date('20160101','yyyymmdd')+1
--(last_day(to_date('20061101','yyyymmdd')) - to_date('20061101','yyyymmdd') +1)
))
select case when max(星期日)=' 01' or max(星期一)=' 01' or max(星期二)=' 01' or max(星期三)=' 01' or max(星期四)=' 01' or max(星期五)=' 01' or max(星期六)=' 01' then year||'年'||month||'月' else '' end,max(星期日) SUN,max(星期一) MON,max(星期二) TUE,max(星期三) WED,max(星期四) THU,max(星期五) FRI,max(星期六) SAT from x group by year,month,week,ym order by year,month,week;