Skip to content

Instantly share code, notes, and snippets.

@ThangLeQuoc
Last active April 16, 2018 13:07
Show Gist options
  • Save ThangLeQuoc/b0f455edf4f09405d3dbcefcc19c8385 to your computer and use it in GitHub Desktop.
Save ThangLeQuoc/b0f455edf4f09405d3dbcefcc19c8385 to your computer and use it in GitHub Desktop.
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 TotalHours
from (select (select sum(hours) from resource) as ResourceHours,
(select sum(hours) from projects-time) as ProjectHours
) t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment