计算一个查询中多个表的行数

时间:2016-03-31 19:38:43

标签: sql oracle

所以我有大量的表属于某一类别。这些表都以B1T开头(所以B1T00001,B1T0002等)。我的任务是提供有关这组表格的一些信息。

这是我的初始SQL,用于计算有多少B1T表。

select obj_nm from od_md_obj
where proj_id = '6' and obj_nm like ('%B1T%')and obj_typ_id = '9';

这会返回大约260个表。我试图计算所有表组合的行数。查询可以按表将其分解,可能使用汇总来计算总数;或者只是对所有事情进行一次计算。

我尝试在我的搜索中查看一些解决方案,但考虑到我查询的表的数量,它需要一些繁琐的代码。

非常感谢任何帮助!!

我目前正在使用SQL开发人员。

2 个答案:

答案 0 :(得分:4)

创建一个将为您生成查询的查询 例如,此查询:

select 'SELECT ''' || table_name || ''' as table_name, count(*) As con ' ||
       'FROM ' || table_name || 
        CASE WHEN lead(table_name) OVER (order by table_name ) IS NOT NULL 
        THEN ' UNION ALL ' END
from user_tables 
where table_name like 'B1T%'
;

将生成如下结果:

SELECT 'B1T00000' as table_name, count(*) As con FROM B1T00000 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00001' as table_name, count(*) As con FROM B1T00001 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00002' as table_name, count(*) As con FROM B1T00002 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00003' as table_name, count(*) As con FROM B1T00003 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00004' as table_name, count(*) As con FROM B1T00004 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00005' as table_name, count(*) As con FROM B1T00005 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00006' as table_name, count(*) As con FROM B1T00006 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00007' as table_name, count(*) As con FROM B1T00007 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00008' as table_name, count(*) As con FROM B1T00008 UNION ALL  
....
....
....
SELECT 'B1T00257' as table_name, count(*) As con FROM B1T00257 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00258' as table_name, count(*) As con FROM B1T00258 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00259' as table_name, count(*) As con FROM B1T00259 UNION ALL                                                                                                                                                                                                                                                  
SELECT 'B1T00260' as table_name, count(*) As con FROM B1T00260

现在只需复制此结果,将其粘贴到您的客户端(SQL-Developer等)并运行它 您将获得所需的结果:

TABLE_NAME        CON
---------- ----------
B1T00000           65 
B1T00001           66 
B1T00002           67 
B1T00003           68 
B1T00004           69
...
...
...
B1T00256          321 
B1T00257          322 
B1T00258          323 
B1T00259          324 
B1T00260          325 

 261 rows selected  

答案 1 :(得分:1)

哪个版本的Oracle? Oracle 12允许在with子句中使用函数;所以可能的解决方案是

WITH FUNCTION count_rows(t_name IN VARCHAR2) RETURN NUMBER  
AS  
   l_rows NUMBER;  
BEGIN  
   EXECUTE IMMEDIATE 'SELECT COUNT(*)  FROM ' || t_name INTO l_rows;  
   RETURN l_rows;  
END;  
SELECT table_name, count_rows(table_name) as row_count
   FROM user_tables 
   WHERE table_name LIKE 'B1T%'

在早期版本中,您可以对必须首先编译的PL / SQL函数执行相同操作,如下所示:

CREATE OR REPLACE FUNCTION count_rows(t_name IN VARCHAR2) RETURN NUMBER  
AS  
  l_rows NUMBER;  
BEGIN  
  EXECUTE IMMEDIATE 'SELECT COUNT(*)  FROM ' || t_name INTO l_rows;  
  RETURN l_rows;  
END;  
/ 

编译它,然后在与上面相同的SELECT语句中使用它。

如果您只需要总计,则可以使用

SELECT SUM(row_count) FROM (SELECT .....)

其中子查询是第一个代码段中显示的查询。