Oracle

將值傳遞到列以在 Excel 中創建圖形

  • May 8, 2020

我正在嘗試將句子的結果轉換為列以在 excel 中創建圖表。

我有這個 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;

我得到這個輸出

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

但我想要這樣的輸出

  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  

這可能嗎?

謝謝。

你想要一個 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;

引用自:https://dba.stackexchange.com/questions/266747