[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2908
  • Last Modified:

SQL Query - Joining two queries as one

I have the following two queries:

SELECT accounts.id, count(*) as totalpeople FROM people
RIGHT JOIN accounts ON people.accountid = accounts.id
WHERE accounts.user='nd2007'
GROUP BY accounts.id

SELECT accounts.id, count(*) as totalchoices FROM choices
RIGHT JOIN people ON choices.peopleid = people.id
RIGHT JOIN accounts ON people.accountid = accounts.id
WHERE accounts.user='nd2007'
GROUP BY accounts.id

This is the result of each:

id,totalpeople
5479,15

id,totalchoices
5479,300

How can I have one query which generates the result:
id,totalpeople,totalchoices
5479,15,300

The table relationships are as follows:
accounts(id,username,...)
people(id,accountid,...) related to the accounts table
choices(id,peopleid,...) related to the people table

Thanks!
0
adrian78
Asked:
adrian78
1 Solution
 
ragoranCommented:
save the query with distinct name (e.g. qryCountPeople and qryCountchoice)
then

select a.ID, TotalPeople, totalchoices
from qryCountPeople as A join on qryCountChoice as B on A.ID = B.ID
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT accounts.id, count(*) as totalpeople, NULL as totalchoices  FROM people
RIGHT JOIN accounts ON people.accountid = accounts.id
WHERE accounts.user='nd2007'
GROUP BY accounts.id
union
SELECT accounts.id, null, count(*) as totalchoices FROM choices
RIGHT JOIN people ON choices.peopleid = people.id
RIGHT JOIN accounts ON people.accountid = accounts.id
WHERE accounts.user='nd2007'
GROUP BY accounts.id
0
 
adrian78Author Commented:
Thanks for the response.  ragoran, if you can provide actual syntax that would be great.

aneeshattingal, that query results in two rows of three columns instead of one row of three:

id,totalpeople,totalchoices
5479,NULL,300
5479,15,NULL
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ragoranCommented:
This is the actual syntax once you save the two queries:

select a.ID, TotalPeople, totalchoices
from qryCountPeople as A join on qryCountChoice as B on A.ID = B.ID
0
 
adrian78Author Commented:
Thanks ragoran... can you include everything including the syntax for saving the query?
0
 
ragoranCommented:
From the Access database dialog window, goto Query, click on new.

You will be presented with a query editor tools and a popup dialog to select a table.   close the dialog without selecting any table.  You will notice a button in the tool bar to switch to SQL panel.  Click on it than copy paste the first query.  From the menu, you can then save the query giving it the name I suggested (or any other).

Close the window and repeat for the second query.

You can then use the joining query I suggested.  If you change the name under which you save the queries, then you will need to update this query as well to use your query name.
0
 
ragoranCommented:
Hmmm, i am not sure you are in Access....

If you are in SQL Server, you can save the query as views.

This is another way to combine the queries without saving.  It is not my preference because it create a more complex query:


select a.ID, TotalPeople, totalchoices
from (
SELECT accounts.id, count(*) as totalpeople FROM people
RIGHT JOIN accounts ON people.accountid = accounts.id
WHERE accounts.user='nd2007'
GROUP BY accounts.id ) as A inner join on
(SELECT accounts.id, count(*) as totalchoices FROM choices
RIGHT JOIN people ON choices.peopleid = people.id
RIGHT JOIN accounts ON people.accountid = accounts.id
WHERE accounts.user='nd2007'
GROUP BY accounts.id )  as B on A.ID = B.ID
0
 
jogosCommented:
right joins always feel unnatural to me, especialy when used by count(*)

SELECT accounts.id, count(*) as totalpeople FROM people
-> this let me think you're counting people

RIGHT JOIN accounts ON people.accountid = accounts.id
-> this says the right table (accounts) must be there, not the left (people)

Conclusion, you're not always counting people, because when no people record found for an accounts-record you are counting aswel.
0
 
adrian78Author Commented:
ragoran, that query worked great.  I just had to remove the "on" after the inner join.
0
 
ragoranCommented:
sorry, Access syntax creeps in and there...
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now