create table pivot (dates date);
insert into pivot (dates) select to_date('01/05/2023', 'DD/MM/YYYY') from dual;
insert into pivot (dates) select to_date('02/05/2023', 'DD/MM/YYYY') from dual;
insert into pivot (dates) select to_date('03/05/2023', 'DD/MM/YYYY') from dual;
insert into pivot (dates) select to_date('04/05/2023', 'DD/MM/YYYY') from dual;
insert into pivot (dates) select to_date('05/05/2023', 'DD/MM/YYYY') from dual;
insert into pivot (dates) select to_date('06/05/2023', 'DD/MM/YYYY') from dual;
insert into pivot (dates) select to_date('07/05/2023', 'DD/MM/YYYY') from dual;
这是查询:
select f.kpi, f.dimension, f.filter, p.dates,
case p.dates
when to_date('01/05/2023', 'DD/MM/YYYY') then f."01/05/2023"
when to_date('02/05/2023', 'DD/MM/YYYY') then f."02/05/2023"
when to_date('03/05/2023', 'DD/MM/YYYY') then f."03/05/2023"
when to_date('04/05/2023', 'DD/MM/YYYY') then f."04/05/2023"
when to_date('05/05/2023', 'DD/MM/YYYY') then f."05/05/2023"
when to_date('06/05/2023', 'DD/MM/YYYY') then f."06/05/2023"
when to_date('07/05/2023', 'DD/MM/YYYY') then f."07/05/2023"
end as val
from foo f cross join pivot p
order by f.kpi, p.dates;
使用以下测试表和数据:
我创建了一个用于交叉连接表 foo 的数据透视表:
这是查询:
这是结果集: