Search code examples
sqljet

combine 2 sql select count statements


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)?


Solution

  • 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