第一关:
select concat(right(attr, 2), substring(attr from 3 for 2)) from level1;
第二关:
with tmp as (select num, string_agg(content, ' ') str
from (select num, array_to_string(array_agg(concat(guo, qing, kuai, le)), '') as content
from (select row_number() over (PARTITION BY attr) num,
CASE attr WHEN '国' THEN '国' ELSE '' END guo,
CASE attr WHEN '庆' THEN '庆' ELSE '' END qing,
CASE attr WHEN '快' THEN '快' ELSE '' END kuai,
CASE attr WHEN '乐' THEN '乐' ELSE '' END le
from level2) as rn
group by num, guo, qing, kuai, le
order by num, guo desc, qing desc, kuai desc, le desc) as nc
group by num
order by num)
select ' chart ' chart union all
select '-------------------' chart union all
select chart
from (select format('%2s | %s |', lpad(concat(tmp.num), 2, '0'),
concat(CASE
WHEN position('国' in str) = 0 THEN ' '
ELSE (CASE
WHEN position('庆' in str) <> 0 or position('快' in str) <> 0 or
position('乐' in str) <> 0 THEN ''
ELSE str END) END,
CASE
WHEN position('庆' in str) = 0 THEN ' '
ELSE (CASE
WHEN position('快' in str) <> 0 or position('乐' in str) <> 0 THEN ''
ELSE str END) END,
CASE
WHEN position('快' in str) = 0 THEN ' '
ELSE (CASE WHEN position('乐' in str) <> 0 THEN '' ELSE str END) END,
CASE WHEN position('乐' in str) = 0 THEN ' ' ELSE str END)) chart
from tmp
order by num desc) as tc
union all
select '-------------------' chart
union all
select ' 2022-10-01 ' chart;