如何调整我的联合查询以提供合并值

时间:2019-03-05 01:08:29

标签: sql oracle union

我有以下查询。它通过db_link调整“驱动程序表”中列出的每个模式的所有索引,lob和表段的大小。它似乎工作得很好,但是,我希望为每个模式一起提供一个大小,而不是给每个表一个值“ T”,给每个索引“ I”和每个LOB“ L”, 。因此,要将这些值合并为一个总计。有人可以帮忙吗?我到目前为止的SQL如下:

select /*+parallel*/ 'DB1' link,
s.owner,
s.segment_name,
'T' AS FLAG,
sum(s.bytes)/1024/1024 AS SUM_BYTES
from DBA_SEGMENTS@DB1 s
where s.owner = (select distinct productionschema from tables_list_tbl)
and s.segment_name in (select table_name
from tables_list_tbl) and
s.segment_type LIKE '%TABLE%'
group by s.owner, s.segment_name
union all
select 'DB1' link, i.table_owner AS OWNER,
i.table_name AS SEGMENT_NAME,
'I' AS FLAG,
sum(s.bytes)/1024/1024 AS SUM_BYTES
from DBA_SEGMENTS@DB1 s,
DBA_INDEXES@DB1 i
where i.table_owner  = (select distinct productionschema from tables_list_tbl) and
i.table_name in (select table_name
from tables_list_tbl) and
i.owner = s.owner and
i.index_name = s.segment_name and
s.segment_type like '%INDEX%'
group by i.table_owner, i.table_name
union all
select 'DB1' link, l.owner,
l.table_name AS SEGMENT_NAME,
'L' AS FLAG,
sum(s.bytes)/1024/1024 AS SUM_BYTES
from DBA_SEGMENTS@DB1 s,
ALL_LOBS@DB1 l
where l.owner = (select distinct productionschema from tables_list_tbl) and
l.table_name in (select table_name
from tables_list_tbl) and
l.owner = s.owner and
l.segment_name = s.segment_name
group by l.owner, l.table_name
order by 1, 2;

0 个答案:

没有答案