2013-04-16 61 views
-3

我有这个疑问:SQL查询,如何添加COUNT(*)列

select segment_name,owner,blocks*8192/1024/1024 as MB,tablespace_name 
from dba_segments 
where segment_name like 'AUD_2%' and owner like 'AUDITOR' 
order by 1 desc; 

SEGMENT_NAME  OWNER    MB TABLESPACE_NAME 
---------------- ---------- ---------- ---------------- 
AUD_201304  AUDITOR    7 WSS  
AUD_201303  AUDITOR   12 WSS  
AUD_201302  AUDITOR   11 WSS 

如何添加COUNT(*)列?

我想一个相关的子查询会做,但究竟是如何?

谢谢!

对不起在stackoverflow上找到代码,下次应该更好地搜索。感谢

对不起,这里的链接解决方案: How to count(*) of multiple tables, size and tablespace in one query

和这里的代码:

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_2%' and owner like 'AUDITOR' 
ORDER BY ut.table_name DESC; 

这里输出:

TABLE_NAME      ROW_COUNT   MB TABLES 
------------------------------ ---------- ---------- ------ 
AUD_201304       21067   7 WSS 
AUD_201303       43198   12 WSS 
AUD_201302       39046   11 WSS 
AUD_201301       44523   17 WSS 
AUD_201212       50580   15 WSS 
AUD_201211       49589   14 WSS 
+2

你正在使用什么RDBMS? –

+0

@MahmoudGamal:由于查询中有'dba_segments',我猜测它是Oracle。 –

+2

你要计算什么? –

回答

2

尝试:

select 
     segment_name, 
     owner, 
     blocks*8192/1024/1024 as MB, 
     tablespace_name, 
     (select num_rows from dba_tables where table_name=segment_name) TOTAL_ROWS 
from dba_segments 
where segment_name like 'AUD_2%' and owner like 'AUDITOR' 
order by 1 desc; 
+0

对不起,这不起作用,列给出的是表的数量,而不是列表中的记录数每桌 – user2286473

+0

请检查编辑答案。 – TechDo

-1

您可以在查询结束时检索@@ ROW_COUNT

+0

不是在甲骨文你不能 – APC

+0

我虽然是一个SQL Server的问题......那么,甲骨文对应M $ @@ ROW_COUNT? – Serge

+0

@@ ROW_COUNT等同于Oracle的SQL%ROWCOUNT。我的MSSQL有点生疏,但我不认为你可以按照你的建议在查询中使用@@ ROW_COUNT。当然,SQL%ROWCOUNT只是返回受前一个DML语句影响的行数(即更新行数,删除行数)。 – APC

0

您不清楚要计数什么。但是,如果你要计算返回的行数,然后使用分析功能:

select segment_name, owner, blocks*8192/1024/1024 as MB, tablespace_name, 
     count(*) over() as cnt 
from dba_segments 
where segment_name like 'AUD_2%' and owner like 'AUDITOR' 
order by 1 desc; 
0

“用表,而不是在每一个表中的记录数的数量出栏”

Youy正在混合两个不同的概念。数据和元数据。

您所查询的是queryiung数据字典,以获取有关表格的一些信息作为数据库中的对象。这是元数据:关于数据的数据。

而每个表所占的行数只是数据。

您有两种选择。首先是将DBA_TABLES视图加入到您的查询中,然后选择NUM_ROWS。如果你的统计数据合理清新,你只需要一个指示性数字,这可能就足够了。

如果您不使用这些表的统计信息,或者想要高度精确的计数,则需要使用PL/SQL。

create or replace function tab_row_cnt (tname in user_tables.table_Nmae%type) 
return pls_integer 
is 
    n pls_integer; 
begin 
    execute immediate 'select count(*) from '||tname into n; 
    return n; 
end; 

您可以在查询的投影中包含此函数。

select segment_name,owner,blocks*8192/1024/1024 as MB,tablespace_name 
     , tab_row_cnt (segment_name) as row_count 
from dba_segments 
where segment_name like 'AUD_2%' and owner like 'AUDITOR' 
and segment_type = 'TABLE' 
order by 1 desc; 

请注意,我在SEGMENT_TYPE上添加了一个测试。如果您的表已分区,或者您想在查询中包含索引段,则需要修改函数的逻辑。

请注意,如果您的表格数量过大可能需要很长时间并显着减慢您的查询速度。速度是使用USER_TABLES.NUM_ROWS提供的近似值的另一个优点。