如何按表计算所有表中的列数和行数?

时间:2012-12-16 17:14:08

标签: sql oracle count

我在查询中找不到错误:

SELECT
u.Table_Name, 
count(distinct c.Column_Name), 
sum(u.num_rows)    
FROM User_Tab_Columns c, User_Tables u    
WHERE   u.TABLE_NAME = c.TABLE_NAME    
group by u.Table_Name;

结果是:

TABLE_NAME                     COUNT(DISTINCTC.COLUMN_NAME) SUM(U.NUM_ROWS)    
------------------------------ ---------------------------- ---------------
DEPT                                                      3              12     
EMP                                                       8             112

应该是:

 TABLE_NAME                     COUNT(DISTINCTC.COLUMN_NAME) SUM(U.NUM_ROWS)    
    ------------------------------ ---------------------------- ---------------
    DEPT                                                      3              4     
    EMP                                                       8             14

所以查询多个行数和列数但我不知道为什么?

1 个答案:

答案 0 :(得分:2)

由于行数是每个表,因此您只需按其分组:

SELECT
u.Table_Name, 
count(*), 
u.num_rows    
FROM User_Tab_Columns c, User_Tables u    
WHERE   u.TABLE_NAME = c.TABLE_NAME    
group by u.Table_Name, u.num_rows;