Конвертировать ROW в столбец с соответствующими записями
мне нужно 4 столбца
TYPE | COUNT | PERIOD | VARIANCE
TRADE 50 JAN 0%
TRADE 100 FEB 100%
Я использую этот запрос и получаю только три столбца
with b1 as
(
SELECT
COUNT(DISTINCT T.SALE)AS TRADE,
TO_CHAR(T.DATE,'YYYY-MON') As Period
FROm TRADE T
JOIN ORDER O ON T.Account = O.Account
WHERE
T.DATE between date '2020-01-01' and date '2020-01-31' --Last
group by TO_CHAR(T.DATE,'YYYY-MON')
UNION ALL
SELECT
COUNT(DISTINCT T.SALE) AS TRADE,
TO_CHAR(T.DATE,'YYYY-MON') As Period
FROm TRADE T
JOIN ORDER O ON T.Account = O.Account
WHERE
T.DATE between date '2020-02-01' and date '2020-02-28' --Last
group by TO_CHAR(T.DATE,'YYYY-MON'))
select TRADE, Period,
100*(TRADE-lag(TRADE,1,TRADE) over (order by period))
/lag(TRADE,1,TRADE) over (order by period)
as "variance(%)"
from b1
order by period