adrian78
asked on
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,totalchoice s
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!
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,totalchoice
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!
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
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
ASKER
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,totalchoice s
5479,NULL,300
5479,15,NULL
aneeshattingal, that query results in two rows of three columns instead of one row of three:
id,totalpeople,totalchoice
5479,NULL,300
5479,15,NULL
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
select a.ID, TotalPeople, totalchoices
from qryCountPeople as A join on qryCountChoice as B on A.ID = B.ID
ASKER
Thanks ragoran... can you include everything including the syntax for saving the query?
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
ragoran, that query worked great. I just had to remove the "on" after the inner join.
sorry, Access syntax creeps in and there...
then
select a.ID, TotalPeople, totalchoices
from qryCountPeople as A join on qryCountChoice as B on A.ID = B.ID