2013-04-16 60 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提供的近似值的另一個優點。