Last active
April 16, 2018 13:07
-
-
Save ThangLeQuoc/b0f455edf4f09405d3dbcefcc19c8385 to your computer and use it in GitHub Desktop.
Add Result of the same query - https://stackoverflow.com/questions/15149775/how-to-add-results-of-two-select-commands-in-same-query?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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