473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

subquery confusion - need help

Greetings,

I am semi-new to Access and have a query question. I presume the solution
is easy, but need some help.

I have created a database with a Contact table. The contact table contains
address fields among other things. Because some contacts share the same
address, I included a boolean field, PrimaryContact. If true, a given
contact's record contains the address info for that contact. If
PrimaryContact is false, then another field, PrimaryContactReference refers
to the ContactID of a different record which contains the address info for
the contact. Make sense so far?

I have been trying to create a query which will seamlessly provide address
info for all contacts whether they are primary contacts or not.

As an example, one field in the query retrieves the city for all contacts.
I tried to use the iif() function to retrieve the necessary info as follows:

City: IIf([tblContact].[PrimaryContact]=True,[tblContact].[City],(SELECT
[tblContact].[City] from tblContact WHERE
[tblContact].[ContactID]=[tblContact].[PrimaryContactReference]))

This works if PrimaryContact=True, but returns nothing (i.e. a blank) if
PrimaryContact=False

If have confirmed that the PrimaryContactReference is accurate, actually
referring to a valid contact, so am not sure why this is not working.

Here is the entire query, including the iif(... subquery...)

SELECT tblContact.FirstName, tblContact.LastName,
tblContact.PrimaryContactReference,
IIf([tblContact].[PrimaryContact]=True,[tblContact].[City],(SELECT
[tblContact].[City] from tblContact WHERE
[tblContact].[ContactID]=PrimaryContactReference)) AS City
FROM tblContact;

Am I on the right track here, or is there a totally different way to do
this?

Thanks in advance for your help.

Ed
Nov 12 '05 #1
2 2985

On Wed, 12 Nov 2003 00:18:02 GMT, "edself" <ed****@ecomail.org> wrote
in comp.databases.ms-access:
City: IIf([tblContact].[PrimaryContact]=True,[tblContact].[City],(SELECT
[tblContact].[City] from tblContact WHERE
[tblContact].[ContactID]=[tblContact].[PrimaryContactReference]))

This works if PrimaryContact=True, but returns nothing (i.e. a blank) if
PrimaryContact=False

If have confirmed that the PrimaryContactReference is accurate, actually
referring to a valid contact, so am not sure why this is not working.
Well, because iif is working as expected (ie, it checks the condition,
and if true, provides the local city field), but subquery's aren't
simply queries embedded anywhere within other queries. More
specifically, ii is a vba construct, and the select statement you are
embedding for use when the flag is false is a sql statement. Iif
can't execute the select statement when the condition is false, which
is why you only get a meaningful result when the flag is true.
Here is the entire query, including the iif(... subquery...)

SELECT tblContact.FirstName, tblContact.LastName,
tblContact.PrimaryContactReference,
IIf([tblContact].[PrimaryContact]=True,[tblContact].[City],(SELECT
[tblContact].[City] from tblContact WHERE
[tblContact].[ContactID]=PrimaryContactReference)) AS City
FROM tblContact;

Am I on the right track here, or is there a totally different way to do
this?


Well, you're sort of on track, but there are several 'right' ways to
do it, and using 'iif' isn't one of them. Here's a workable solution.

SELECT c.FirstName, c.LastName, IIf(PrimaryContact,c.City,p.City)
FROM tblContact as c left join tblContact as p
on c.primarycontactreference=p.contactid;

Another possibility is to set the primarycontactreference field to
equal contactid on records where primarycontact=true, then simply
eliminate the primarycontact boolean field (since that information is
already contained in the test contactid=primarycontactreference?).
The advantage of doing this is that your query would then simply
become:

SELECT c.FirstName, c.LastName, p.City
FROM tblContact as c left join tblContact as p
on c.primarycontactreference=p.contactid;

There's other (better) ways to do this, but that should get you
started.

Peter Miller
__________________________________________________ __________
PK Solutions -- Data Recovery for Microsoft Access/Jet/SQL
Free quotes, Guaranteed lowest prices and best results
www.pksolutions.com 1.866.FILE.FIX 1.760.476.9051
Nov 12 '05 #2
Hi,

You should use a union query.

It will looks like this:

SELECT [Contacts].[ContactID], [Contacts].[Name], [Contacts].[Address]
FROM Contacts
WHERE ((([Contacts].[PrimaryContact])=True))
union
SELECT Contacts.ContactID, Contacts.Name, Contacts_1.Address
FROM Contacts INNER JOIN Contacts AS Contacts_1 ON
Contacts.PrimaryContactReference = Contacts_1.ContactID
WHERE (((Contacts.PrimaryContact)=False));

I selected only a generic, Address field, but obviously you need to select
all fields who makes up your address

The important thing is you need to both Select's to have the same fields, in
the same order.

If you need any further assistance, you can contact me.

HTH,
Bogdan

____________________________
Independent consultant
"edself" <ed****@ecomail.org> wrote in message
news:__esb.174503$Tr4.470011@attbi_s03...
Greetings,

I am semi-new to Access and have a query question. I presume the solution
is easy, but need some help.

I have created a database with a Contact table. The contact table contains address fields among other things. Because some contacts share the same
address, I included a boolean field, PrimaryContact. If true, a given
contact's record contains the address info for that contact. If
PrimaryContact is false, then another field, PrimaryContactReference refers to the ContactID of a different record which contains the address info for
the contact. Make sense so far?

I have been trying to create a query which will seamlessly provide address
info for all contacts whether they are primary contacts or not.

As an example, one field in the query retrieves the city for all contacts.
I tried to use the iif() function to retrieve the necessary info as follows:
City: IIf([tblContact].[PrimaryContact]=True,[tblContact].[City],(SELECT
[tblContact].[City] from tblContact WHERE
[tblContact].[ContactID]=[tblContact].[PrimaryContactReference]))

This works if PrimaryContact=True, but returns nothing (i.e. a blank) if
PrimaryContact=False

If have confirmed that the PrimaryContactReference is accurate, actually
referring to a valid contact, so am not sure why this is not working.

Here is the entire query, including the iif(... subquery...)

SELECT tblContact.FirstName, tblContact.LastName,
tblContact.PrimaryContactReference,
IIf([tblContact].[PrimaryContact]=True,[tblContact].[City],(SELECT
[tblContact].[City] from tblContact WHERE
[tblContact].[ContactID]=PrimaryContactReference)) AS City
FROM tblContact;

Am I on the right track here, or is there a totally different way to do
this?

Thanks in advance for your help.

Ed

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vikram Vaswani | last post by:
Hi all, I have the following two tables: mysql> SELECT * FROM clients; +-----+-----------------------------+ | cid | cname | +-----+-----------------------------+ |...
2
by: Nachi | last post by:
Hi, Urgent Help appreciated.... I am getting resultset with first condition and when try to get the resutlset from second condition i am getting the above error in SQL200. I know that i am...
4
by: Kenny G | last post by:
Below is a query that I currently have. I need to produce a subquery so that the top five of the CodeCount is returned. I appreciate your help. SELECT .PX_SURGEON, .PX_CODE, Count(.PX_CODE)...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
6
by: phillip.s.powell | last post by:
update student s set school_year_id = (select distinct s.id from school_year s, interns i where lower(s.school_year_name) = lower(i.enrollment_year)and s.unique_key = i.unique_key group by s.id);...
2
by: dcourington | last post by:
I'm new to this group and not an experienced Access user, but have solved several of my problems already by reading other threads on this forum (Thanks!). Here's my current problem that I can't...
0
by: kiran2nirvan | last post by:
hi please help in solving this i am recieving this error"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as...
1
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.