【发布时间】:2016-11-15 23:53:06
【问题描述】:
我正在尝试编写一个查询,该查询将为我提供有关用户/模式的信息。
它需要使用以下内容:
COLUMNS = 用户名、default_tablespace、account_status 表 = dba_users
PLUS,结果集中的一个新虚拟列显示该用户名/模式的对象计数。即它拥有的对象的数量。 简单的 where 子句应该是 account_status 是打开的,并且用户名不是 X、Y、Z 之一。
所以它将以某种方式结合以下基本查询:
select username, default_tablespace, account_status
from dba_users
where account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
有了这个:
select owner, count(*) as object_count from dba_objects
group by owner
order by 1
我尝试尝试使用行内查询。我能想到的最好的查询是下面的查询,但这仅列出了 dba_objects 表中存在的用户。即仅列出架构(包含至少 1 个对象)而不是普通用户。
select username, account_status, default_tablespace, subquery1.object_count
from dba_users,
(
select owner,count(*) as object_count from dba_objects
group by owner order by 1
) subquery1
where username = owner
and account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
order by username
我认为我需要执行左外连接,以保留满足其余查询的 dba_users 记录,因此在 object_count 列中仅返回“NULL”,但是当我尝试如下时,我得到垃圾结果,它列出了太多,重复用户多次随机对象计数。
select username, account_status, default_tablespace, subquery1.object_count
from dba_users,
(
select owner, count(*) as object_count from dba_objects
group by owner order by 1
) subquery1
left join dba_users on username = subquery1.owner
and account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
order by username
作为奖励,如果有人能提出一个不仅提供上述内容的查询,而且还提供一个额外的虚拟列,提供所用表空间的大小,那就太好了。所以它会使用类似下面的查询:
选择 round((sum(bytes)/1024/1024/1024),1) 作为 size_in_gb,所有者 来自 dba_segments 通过...分组 所有者
非常感谢。
【问题讨论】:
标签: sql oracle join subquery inline