A Microsoft Office (Excel, Word) forum. OfficeFrustration

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.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Convert SQL query into Access query



 
 
Thread Tools Display Modes
  #1  
Old January 8th, 2007, 06:25 PM posted to microsoft.public.access.queries
[email protected]
external usenet poster
 
Posts: 1
Default 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

  #3  
Old January 8th, 2007, 08:03 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default 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  
Old January 8th, 2007, 08:48 PM posted to microsoft.public.access.queries
Ken Sheridan
external usenet poster
 
Posts: 3,433
Default 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  
Old January 9th, 2007, 10:51 AM posted to microsoft.public.access.queries
Ken Sheridan
external usenet poster
 
Posts: 3,433
Default 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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 02:11 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.