0

I ahave 2 simple count queries:

select count (*) from t_object
select count (*) from t_diagram  

How is the simplest way to combine their result (sum)?

user3165438
  • 2,631
  • 7
  • 34
  • 54

2 Answers2

3

Use UNION ALL to get two different count:

select count (*), 't_object count' from t_object
union all
select count (*), 't_diagram count' from t_diagram

To get the sum of the counts, use a derived table:

select sum(dt.cnt) from
(
 select count(*) as cnt from t_object
 union all
 select count(*) as cnt from t_diagram
) dt

Or, use a sub-query:

select count(*) + (select count(*) from t_diagram) from t_object
jarlh
  • 42,561
  • 8
  • 45
  • 63
2

Depends on what you mean by "combine". To sum them up:

select  (select count (*) from t_object) + count(*) as combined_count
from    t_diagram
Andomar
  • 232,371
  • 49
  • 380
  • 404
  • 1
    Thanks. I get the following error: `The select statement includes a ersversed word or an argument name that misspelled or missing, or the punctuation is incorrect.` Any ideas? – user3165438 Jun 03 '15 at 09:11
  • Looks like you need at least one source table. Jarlh's answer already had that btw. – Andomar Jun 03 '15 at 09:22