I have a table that contain years that and total sales like following
WITH sample_sales_table
AS
(
select 2015 FY, 100 SHOE, 50 SHIRT, 2 HAT from dual
union all
select 2016, 120, 55, 4 from dual
union all
select 2017, 150, 80, 10 from dual
)
SELECT *
FROM sample_sales_table;
Source of origin table
FY SHOE SHIRT HAT
---------- ---------- ---------- ----------
2015 100 50 2
2016 120 55 4
2017 150 80 10
Possibly 2018, 2019 data
How to create following report ?
WITH sales_report
AS
(
select 'SHOE' AS CATEGORY, 100 AS "2015", 120 AS "2016", 150 AS "2017" from dual
union all
select 'SHIRT', 50, 55, 80 from dual
union all
select 'HAT', 2, 4, 10 from dual
)
SELECT *
FROM sales_report;
Desired Report output as following:
CATEGORY 2015 2016 2017
---------- ---------- ---------- ----------
SHOE 100 120 150
SHIRT 50 55 80
HAT 2 4 10
The report may look like this too if new record is added
CATEGORY 2015 2016 2017 2018
---------- ---------- ---------- ---------- ----------
SHOE 100 120 150 180
SHIRT 50 55 80 120
HAT 2 4 10 15
I have tried with PIVOT / UNPIVOT and with the LISTAGG to for the FY just never get it right.
If someone can help
Thanks