How to add results of two select commands in same query How to add results of two select commands in same query sqlite sqlite

How to add results of two select commands in same query


Yes. It is possible :D

SELECT  SUM(totalHours) totalHoursFROM        (             select sum(hours) totalHours from resource            UNION ALL            select sum(hours) totalHours from projects-time        ) s

As a sidenote, the tablename projects-time must be delimited to avoid syntax error. Delimiter symbols vary on RDBMS you are using.


Something simple like this can be done using subqueries in the select clause:

select ((select sum(hours) from resource) +        (select sum(hours) from projects-time)       ) as totalHours

For such a simple query as this, such a subselect is reasonable.

In some databases, you might have to add from dual for the query to compile.

If you want to output each individually:

select (select sum(hours) from resource) as ResourceHours,       (select sum(hours) from projects-time) as ProjectHours

If you want both and the sum, a subquery is handy:

select ResourceHours, ProjectHours, (ResourceHours+ProjecctHours) as TotalHoursfrom (select (select sum(hours) from resource) as ResourceHours,             (select sum(hours) from projects-time) as ProjectHours     ) t


UNION ALL once, aggregate once:

SELECT sum(hours) AS total_hoursFROM   (   SELECT hours FROM resource   UNION ALL   SELECT hours FROM "projects-time" -- illegal name without quotes in most RDBMS   ) x