2

我有 2 个输出正确的工作查询。第一个查询给出表名列表和计数,第二个查询给出表名列表、MB 大小和表空间名称。

如何将它们组合到一个查询中?

查询一:

select table_name
,to_number(extractvalue(xmltype(
dbms_xmlgen.getxml(
'select count(*) c from '||table_name))
,'/ROWSET/ROW/C')) count
from user_tables 
where table_name like 'AUD_2012080%'
order by table_name desc;

查询 2:

select segment_name,blocks*8192/1024/1024 as MB,tablespace_name 
from dba_segments 
where segment_name like 'AUD_2012080%' 
order by segment_name desc;

输出1:

TABLE_NAME                          COUNT
------------------------------ ----------
AUD_20120809                        27967 
AUD_20120808                        28269 
AUD_20120807                        29354 
AUD_20120806                        27155 
AUD_20120805                        18042 
AUD_20120804                        19033 
AUD_20120803                        24642 
AUD_20120802                        26760 
AUD_20120801                        25776 

输出 2:

SEGMENT_NAME                 MB TABLESPACE_NAME              
-------------------- ---------- ------------------------------
AUD_20120809                 35 WSS_BIG_I                      
AUD_20120808                 35 WSS_BIG_I                      
AUD_20120807                 36 WSS_BIG_I                      
AUD_20120806                 34 WSS_BIG_I                      
AUD_20120805                 24 WSS_BIG_I                      
AUD_20120804                 24 WSS_BIG_I                      
AUD_20120803                 29 WSS_BIG_I                      
AUD_20120802                 32 WSS_BIG_I                      
AUD_20120801                 30 WSS_BIG_I  

我正在寻找的结果是:

Table_name    count   owner    MB   Tablespace_name
AUD_20120801  25776   auditor  30   wss_big_i 
...
...  
4

2 回答 2

3

由于段通常像表格一样命名:

SELECT ut.table_name,
       to_number(extractvalue(xmltype (dbms_xmlgen.getxml ('select count(*) c from ' ||ut.table_name)),'/ROWSET/ROW/C')) row_count,
       db.blocks*8192/1024/1024 as MB,
       db.tablespace_name 
FROM user_tables ut
  join dba_segments db on db.segment_name = ut.table_name
WHERE ut.table_name LIKE 'AUD_2012080%'
ORDER BY ut.table_name DESC
于 2012-12-11T15:09:49.000 回答
1

一个解决方案是:

SELECT ut.table_name
, NVL(extractvalue(xmltype (dbms_xmlgen.getxml ('select max(ID) x from ' || ut.table_name)),'/ROWSET/ROW/X'),0) maxID 
FROM all_tables ut
LEFT JOIN cols c ON c.TABLE_NAME = ut.TABLE_NAME
where ut.owner = 'OWNER'
AND c.COLUMN_NAME = 'ID';
于 2015-08-13T12:07:08.763 回答