我如何计算三个表中的行数并返回三个行计数的总和?
答案 0 :(得分:2)
这将返回您的数据库上的每个表计数,只需添加WHERE
来过滤您想要的那些
http://www.dba-oracle.com/t_count_rows_all_tables_in_schema.htm
select
table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c from '||table_name))
,'/ROWSET/ROW/C')) row_count
from
user_tables
order by
table_name;
只需将其添加到CTE
即可获得SUM
WITH cte AS (
select
table_name,
to_number(
extractvalue(
xmltype(
dbms_xmlgen.getxml('select count(*) c from '||table_name))
,'/ROWSET/ROW/C')) row_count
from
user_tables
WHERE table_name in ('table1', 'table2', 'table3' )
)
SELECT SUM(row_count)
FROM cte
答案 1 :(得分:1)
space
答案 2 :(得分:1)
由于表格的行数作为单行结果返回,您可以将三个结果交叉连接,并将它们与+
运算符相加:
SELECT a.cnt + b.cnt + c.cnt
FROM (SELECT COUNT(*) AS cnt FROM table_a) a
CROSS JOIN (SELECT COUNT(*) AS cnt FROM table_b) b
CROSS JOIN (SELECT COUNT(*) AS cnt FROM table_c) c
答案 3 :(得分:1)
select sum(v.cnt) as total_sum
from
(select count (*) as cnt from <<table1>>
union all
select count (*) as cnt from <<table2>>
union all
select count (*) as cnt from <<table3>>) v
答案 4 :(得分:0)
select
cnt1 + cnt2 + cnt3
as mycnt
from
(SELECT COUNT (*) as cnt1 FROM s1.t1),
(SELECT COUNT (*) as cnt2 FROM s1.t2),
(SELECT COUNT (*) as cnt3 FROM s1.t3)
我最终使用的是什么。谢谢大家。