If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Convert SQL query into Access query
I am trying to change this sql statment into a an access sql statement.
I am not sure how convert the where statement "where c.ClientID not in (Select clientID from tblIPS)" into access. select co.CompanyName, count(c.ClientName) NumberOfClientsWithNoIPS from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) Group By CompanyName union select 'zz - Total', count(c.ClientName) Total from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) order by CompanyName Can someone help? Thanks, Alanna |
#2
|
|||
|
|||
Convert SQL query into Access query
|
#3
|
|||
|
|||
Convert SQL query into Access query
That should work with the exception that you will probably need to use the
AS word for your field alias and the Union will ignore the TOTAL alias in the second query clause and will use NumberOfClientsWithNoIPS as the column name. select co.CompanyName , count(c.ClientName) AS NumberOfClientsWithNoIPS from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) Group By CompanyName union select 'zz - Total' , count(c.ClientName) AS Total from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) order by CompanyName -- John Spencer Access MVP 2002-2005, 2007 Center for Health Program Development and Management University of Maryland Baltimore County .. wrote in message oups.com... I am trying to change this sql statment into a an access sql statement. I am not sure how convert the where statement "where c.ClientID not in (Select clientID from tblIPS)" into access. select co.CompanyName, count(c.ClientName) NumberOfClientsWithNoIPS from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) Group By CompanyName union select 'zz - Total', count(c.ClientName) Total from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) order by CompanyName Can someone help? Thanks, Alanna |
#4
|
|||
|
|||
Convert SQL query into Access query
Alanna:
There seem to be a lot of unnecessary aliases and subqueries here. I'd have thought it could be simplified to: SELECT CompanyName, COUNT(*) AS NumberOfClientsWithNoIPS FROM tblClients, tblCompanies, tblIPS WHERE tblClients.RIACompanyID = tblCompanies.CompanyID AND tblClients.ClientID = tblIPS.ClientID GROUP BY CompanyName UNION ALL SELECT 'zz - Total', COUNT(*) FROM tblClients, tblCompanies, tblIPS WHERE tblClients.RIACompanyID = tblCompanies.CompanyID AND tblClients.ClientID = tblIPS.ClientID ORDER BY CompanyName; Ken Sheridan Stafford, England " wrote: I am trying to change this sql statment into a an access sql statement. I am not sure how convert the where statement "where c.ClientID not in (Select clientID from tblIPS)" into access. select co.CompanyName, count(c.ClientName) NumberOfClientsWithNoIPS from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) Group By CompanyName union select 'zz - Total', count(c.ClientName) Total from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) order by CompanyName Can someone help? Thanks, Alanna |
#5
|
|||
|
|||
Convert SQL query into Access query
Alanna :
Oops! It was NOT IN, so the subqueries *are* necessary. I'd suggest using the NOT EXISTS predicate, however. SQL programmers generally favour the latter on the basis that it allows the optimizer maximum discretion in the use of the indexes: SELECT CompanyName, COUNT(*) AS NumberOfClientsWithNoIPS FROM tblClients, tblCompanies, tblIPS WHERE tblClients.RIACompanyID = tblCompanies.CompanyID AND NOT EXISTS (SELECT * FROM tblIPS WHERE tblIPS.ClientID = tblClients.ClientID) GROUP BY CompanyName UNION ALL SELECT 'zz - Total', COUNT(*) FROM tblClients, tblCompanies, tblIPS WHERE tblClients.RIACompanyID = tblCompanies.CompanyID AND NOT EXISTS (SELECT * FROM tblIPS WHERE tblIPS.ClientID = tblClients.ClientID) ORDER BY CompanyName; Ken Sheridan Stafford, England " wrote: I am trying to change this sql statment into a an access sql statement. I am not sure how convert the where statement "where c.ClientID not in (Select clientID from tblIPS)" into access. select co.CompanyName, count(c.ClientName) NumberOfClientsWithNoIPS from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) Group By CompanyName union select 'zz - Total', count(c.ClientName) Total from tblClients c join tblCompanies co on c.RIACompanyID = co.CompanyID where c.ClientID not in (Select clientID from tblIPS) order by CompanyName Can someone help? Thanks, Alanna |
Thread Tools | |
Display Modes | |
|
|