Pass values to column to create graphs in excel
-
01-03-2021 - |
Question
I'm trying to convert the result of a sentence into columns to create a graph in excel.
I have this sql
select
hss.instance_number INST,
to_char(snap.begin_interval_time,'dd/mm/yyyy hh24:mi:ss') as Snap_Inicial,
hss.pool,hss.name,trunc(hss.bytes/1024/1024,2) SIZE_IN_MB
FROM DBA_HIST_SGASTAT hss, dba_hist_snapshot snap
WHERE snap.instance_number = hss.instance_number
and hss.snap_id=snap.snap_id
and hss.pool='shared pool'
and hss.instance_number=1
and hss.pool is not null
and BEGIN_INTERVAL_TIME BETWEEN sysdate-5 and sysdate
and hss.name in ('KGLHD','SQLA','KGLS')
ORDER BY INST, BEGIN_INTERVAL_TIME;
and I get this output
INST SNAP_INICIAL POOL NAME SIZE_IN_MB
---- ------------------- ------------- -------------- ----------
1 03/05/2020 09:00:30 shared pool SQLA 150
1 03/05/2020 09:00:30 shared pool KGLS 23
1 03/05/2020 09:00:30 shared pool KGLHD 14
1 03/05/2020 09:30:32 shared pool SQLA 160
1 03/05/2020 09:30:32 shared pool KGLS 33
1 03/05/2020 09:30:32 shared pool KGLHD 10
but I want an output like this
INST SNAP_INICIAL POOL SQLA KGLS KGLHD
---- ------------------- ------------- ----------- -------- ---------
1 03/05/2020 09:00:30 shared pool 150 23 14
1 03/05/2020 09:30:32 shared pool 160 33 10
Is this posible?
Thanks.
La solution
You want an Oracle PIVOT
.
SELECT * FROM (
SELECT hss.instance_number AS inst,
snap.begin_interval_time AS snap_inicial,
hss.pool,
hss.name,
TRUNC(hss.bytes / 1024 / 1024, 2) AS size_in_mb
FROM dba_hist_sgastat hss
JOIN dba_hist_snapshot snap
ON snap.instance_number = hss.instance_number
AND snap.snap_id = hss.snap_id
WHERE hss.pool = 'shared pool'
AND hss.instance_number = 1
AND hss.pool IS NOT NULL
AND snap.begin_interval_time BETWEEN SYSDATE - 5 AND SYSDATE
AND hss.name IN ('KGLHD', 'SQLA', 'KGLS')
)
PIVOT (
SUM(size_in_mb) FOR name IN ('KGLHD', 'SQLA', 'KGLS')
)
ORDER BY inst ASC, snap_inicial ASC;
Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange