Rhys Rhys - 1 year ago 84
SQL Question

How to add results of two select commands in same query

I currently have two select commands as per below. What I would like to do is to add the results together in the SQL query rather than the variables in code.

select sum(hours) from resource;
select sum(hours) from projects-time;

Is it possible to have both in the same SQL and output a sum of both results?

Answer Source

Yes. It is possible :D

SELECT  SUM(totalHours) totalHours
            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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download