473,463 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with a Derived Table SQL query in Access

Hi All

I wonder if anyone can help me with this.
The scenario is that I have a pair of related tables. One contains
record labels, the other contains contact names at those labels. In
the contacts table there is a boolean field called blnLabelDefault
which identifies whether the listed contact should be used as the
default for a label. There is code to ensure that no more than one
contact may be listed as the default for any given label. However a
label might not have ANY contacts listed as default.
I am trying to create a query which will show basic info from the
tblLabels table and basic details from the FIRST record only of the
tblLabelContacts table, which is sorted by blnLabelDefault. The
theory
is that this will show me the the label details, plus the info for
only the default contact if there is one, but if not then only the
first contact for the label. Ultimately, the data will move to a
position where every label has a default contact.
The way I have approached it is to create a SQL statement which joins
tblLabels with a derived table, called TempContacts, which shows the
top 1 contact, ordered by blnLabelDefault, where the label ID matches
the label ID for the outer query. The full statement is below.
However when I run the query, I am asked to supply a value for
lngLabelID. If I don't, or if I supply a non-existant LabelID, I get
no results, if I supply an actual LabelID, I get effectively a cross-
join query, where I see the correct details for the contacts at the
valid label ID that I supplied, next to every single label in the
table.
All field names are correct and double-checked!
Help!
Thanks a lot
Andrew
Select Labels.lngLabelID, strLabelName, blnActive,
tempContacts.ContactName
from tblLabels as Labels
left outer JOIN
(
SELECT top 1 lngContactID, lngLabelID, [strcontactfirstname] & " " &
[strcontactlastname] AS ContactName, strContactEmail, strPhone,
strContactType, blnLabelDefault
FROM tblLabelContacts
WHERE tempContacts.lngLabelID=Labels.lngLabelID
ORDER BY blnLabelDefault
) AS tempContacts
on Labels.lngLabelID=tempContacts.lnglabelID

Jun 12 '07 #1
3 2850
On Jun 12, 7:24 am, Andrew <andrewmricha...@yahoo.co.ukwrote:
Hi All

I wonder if anyone can help me with this.

The scenario is that I have a pair of related tables. One contains
record labels, the other contains contact names at those labels. In
the contacts table there is a boolean field called blnLabelDefault
which identifies whether the listed contact should be used as the
default for a label. There is code to ensure that no more than one
contact may be listed as the default for any given label. However a
label might not have ANY contacts listed as default.

I am trying to create a query which will show basic info from the
tblLabels table and basic details from the FIRST record only of the
tblLabelContacts table, which is sorted by blnLabelDefault. The
theory
is that this will show me the the label details, plus the info for
only the default contact if there is one, but if not then only the
first contact for the label. Ultimately, the data will move to a
position where every label has a default contact.

The way I have approached it is to create a SQL statement which joins
tblLabels with a derived table, called TempContacts, which shows the
top 1 contact, ordered by blnLabelDefault, where the label ID matches
the label ID for the outer query. The full statement is below.

However when I run the query, I am asked to supply a value for
lngLabelID. If I don't, or if I supply a non-existant LabelID, I get
no results, if I supply an actual LabelID, I get effectively a cross-
join query, where I see the correct details for the contacts at the
valid label ID that I supplied, next to every single label in the
table.

All field names are correct and double-checked!

Help!

Thanks a lot
Andrew

Select Labels.lngLabelID, strLabelName, blnActive,
tempContacts.ContactName
from tblLabels as Labels
left outer JOIN

(
SELECT top 1 lngContactID, lngLabelID, [strcontactfirstname] & " " &
[strcontactlastname] AS ContactName, strContactEmail, strPhone,
strContactType, blnLabelDefault
FROM tblLabelContacts
WHERE tempContacts.lngLabelID=Labels.lngLabelID
ORDER BY blnLabelDefault
) AS tempContacts

on Labels.lngLabelID=tempContacts.lnglabelID
If there is no default contact what is the determining factor as to
which contact is 'first'?

Jun 12 '07 #2
..lngLabelID=tempContacts.lnglabelID
>
If there is no default contact what is the determining factor as to
which contact is 'first'?- Hide quoted text -

- Show quoted text -
There's no preferred contact as such, so it would presumably default
to whatever comes up first as ordered by underlying index settings.

Andrew

Jun 12 '07 #3
You might want to store your derived table SQL as a stored query and look at
the results of it alone. Then, if they're fine, join that stored query to
the other table in your below SQL, and so on. That's an easy way to
troubleshoot it.

"Andrew" <an*************@yahoo.co.ukwrote in message
news:11**********************@a26g2000pre.googlegr oups.com...
Hi All

I wonder if anyone can help me with this.
The scenario is that I have a pair of related tables. One contains
record labels, the other contains contact names at those labels. In
the contacts table there is a boolean field called blnLabelDefault
which identifies whether the listed contact should be used as the
default for a label. There is code to ensure that no more than one
contact may be listed as the default for any given label. However a
label might not have ANY contacts listed as default.
I am trying to create a query which will show basic info from the
tblLabels table and basic details from the FIRST record only of the
tblLabelContacts table, which is sorted by blnLabelDefault. The
theory
is that this will show me the the label details, plus the info for
only the default contact if there is one, but if not then only the
first contact for the label. Ultimately, the data will move to a
position where every label has a default contact.
The way I have approached it is to create a SQL statement which joins
tblLabels with a derived table, called TempContacts, which shows the
top 1 contact, ordered by blnLabelDefault, where the label ID matches
the label ID for the outer query. The full statement is below.
However when I run the query, I am asked to supply a value for
lngLabelID. If I don't, or if I supply a non-existant LabelID, I get
no results, if I supply an actual LabelID, I get effectively a cross-
join query, where I see the correct details for the contacts at the
valid label ID that I supplied, next to every single label in the
table.
All field names are correct and double-checked!
Help!
Thanks a lot
Andrew
Select Labels.lngLabelID, strLabelName, blnActive,
tempContacts.ContactName
from tblLabels as Labels
left outer JOIN
(
SELECT top 1 lngContactID, lngLabelID, [strcontactfirstname] & " " &
[strcontactlastname] AS ContactName, strContactEmail, strPhone,
strContactType, blnLabelDefault
FROM tblLabelContacts
WHERE tempContacts.lngLabelID=Labels.lngLabelID
ORDER BY blnLabelDefault
) AS tempContacts
on Labels.lngLabelID=tempContacts.lnglabelID

Jun 13 '07 #4

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

Similar topics

6
by: Bill | last post by:
I've created this: SELECT c.ProjectID, Count(c.ID) as 'Registrants', Count(dt.Hits) as 'Submissions' FROM CME_TBL c JOIN (SELECT ProjectID, Count(*) as Hits FROM CME_TBL
2
by: Keith B via SQLMonster.com | last post by:
Hi! I want to return a derived table along with 4 simple tables in a stored procedure as follows: Input parameter: @FtNum (==Order Number, selects one Order and all associated data)...
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...
14
by: marcokrechting | last post by:
Hi All, I have a rather complex math problem concerning dates. I will try to explain my problem. I have a table with the fields SUBJECT (text), DUE DATE (date) and CHECKED (yes/no). In this...
1
by: cindy | last post by:
Get data into datatable, add to dataset dsSearch " Get data into datatable, add to dataset dsSearch Using In-Memory SQL Engine join the tables and select the filenames from the join, add to...
6
by: PW | last post by:
I've created an ASP application which uses an Access database. I've created an outer join query, but for some reason the "Property_Def" column is not aligning with the "ESPN" column. They should...
2
by: johnhanis | last post by:
I'm using a Visual Basic front end with an SQL query to select some data from a MS Access database. I have a table named Tithes with Columns of TitheDate Tither No Total Tithes Faith Promise...
12
by: info | last post by:
The following query works fine in access and does exactly what I want it to do however I get a syntax error when I port it over to SQL Server 2000. ------------- UPDATE OrdersProducts INNER...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.