473,385 Members | 1,402 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,385 software developers and data experts.

Need SQL for Joined Tables

Hello

I have the following tables joined on a one (Field4) to many (Field3)
relationship

Table1
Field1 / Field2 / Field3
1 Bob 2
2 Fred 4
3 Paul 2
4 John 3
(AutoNum)

Table2
Field4 / Field5
1 Doctor
2 Dentist
3 Lawyer
4 Teacher
(AutoNum)

The following SQL will output :

SELECT Table1.Field1, Table1.Field2, Table1.Field3, Table2.Field4,
Table2.Field5
FROM Table2 INNER JOIN Table1 ON Table2.Field4 = Table1.Field3;

Field1 / Field 2 / Field3 / Field4 /
Field5
1 Bob 2 2
Dentist
3 Paul 2 2
Dentist
2 Fred 4 4
Teacher
4 John 3 3
Lawyer

How can I write an SQL so that only one record with Dentist (in Field5) is
output
with either Bob or Paul but not both?
Example:

Field1 / Field 2 / Field3 / Field4 /
Field5
1 Bob 2 2
Dentist
2 Fred 4 4
Teacher
4 John 3 3
Lawyer
I am using Access 2000

Thanks
G Gerard

Nov 13 '05 #1
2 1505
Hi,
This works, it's a bit long, so maybe someone else will offer a better
solution...

Use 4 separate queries, one for each profession, retrieve the "TOP 1" row
from each and then UNION the results together.

In Access Help search for this: "Create an SQL-specific query"
It describes how to create a union query in the query window.

tblProfession
ProfessionId
ProfessionName

tblPerson
PersonID
ProfessionID
PersonName

SQL for Query1
SELECT TOP 1 tblPerson.PersonName, tblProfession.ProfessionName
FROM tblProfession INNER JOIN tblPerson ON tblProfession.ProfessionId =
tblPerson.ProfessionID
WHERE (((tblProfession.ProfessionName)="Doctor"));

SQL for Query2
SELECT TOP 1 tblPerson.PersonName, tblProfession.ProfessionName
FROM tblProfession INNER JOIN tblPerson ON tblProfession.ProfessionId =
tblPerson.ProfessionID
WHERE (((tblProfession.ProfessionName)="Dentist"));

(Queries 3 and 4 same style)

SQL for Query5
SELECT PersonName, ProfessionName FROM Query1
union
SELECT PersonName, ProfessionName FROM Query2
union
SELECT PersonName, ProfessionName FROM Query3
union
SELECT PersonName, ProfessionName FROM Query4
;

Query5 output PersonName ProfessionName
Bob Dentist
Fred Teacher
John Lawyer
HTH -Linda
"GGerard" <gg*****@nbnet.nb.ca> wrote in message
news:Ls*********************@ursa-nb00s0.nbnet.nb.ca...
Hello

I have the following tables joined on a one (Field4) to many (Field3)
relationship

Table1
Field1 / Field2 / Field3
1 Bob 2
2 Fred 4
3 Paul 2
4 John 3
(AutoNum)

Table2
Field4 / Field5
1 Doctor
2 Dentist
3 Lawyer
4 Teacher
(AutoNum)

The following SQL will output :

SELECT Table1.Field1, Table1.Field2, Table1.Field3, Table2.Field4,
Table2.Field5
FROM Table2 INNER JOIN Table1 ON Table2.Field4 = Table1.Field3;

Field1 / Field 2 / Field3 / Field4 /
Field5
1 Bob 2 2
Dentist
3 Paul 2 2
Dentist
2 Fred 4 4
Teacher
4 John 3 3
Lawyer

How can I write an SQL so that only one record with Dentist (in Field5) is
output
with either Bob or Paul but not both?
Example:

Field1 / Field 2 / Field3 / Field4 /
Field5
1 Bob 2 2
Dentist
2 Fred 4 4
Teacher
4 John 3 3
Lawyer
I am using Access 2000

Thanks
G Gerard

Nov 13 '05 #2
"GGerard" <gg*****@nbnet.nb.ca> wrote in message news:<Ls*********************@ursa-nb00s0.nbnet.nb.ca>...
Hello

I have the following tables joined on a one (Field4) to many (Field3)
relationship

Table1
Field1 / Field2 / Field3
1 Bob 2
2 Fred 4
3 Paul 2
4 John 3
(AutoNum)

Table2
Field4 / Field5
1 Doctor
2 Dentist
3 Lawyer
4 Teacher
(AutoNum)

The following SQL will output :

SELECT Table1.Field1, Table1.Field2, Table1.Field3, Table2.Field4,
Table2.Field5
FROM Table2 INNER JOIN Table1 ON Table2.Field4 = Table1.Field3;

Field1 / Field 2 / Field3 / Field4 /
Field5
1 Bob 2 2
Dentist
3 Paul 2 2
Dentist
2 Fred 4 4
Teacher
4 John 3 3
Lawyer

How can I write an SQL so that only one record with Dentist (in Field5) is
output
with either Bob or Paul but not both?
Example:

Field1 / Field 2 / Field3 / Field4 /
Field5
1 Bob 2 2
Dentist
2 Fred 4 4
Teacher
4 John 3 3
Lawyer
I am using Access 2000

Thanks
G Gerard


My post yesterday bounced so this time I'll make it shorter.

SELECT First(Table1.Field1) AS Field1, First(Table1.Field2) AS Field2,
Table1.Field3, Table2.Field4ID, Table2.Field5
FROM Table1 INNER JOIN Table2 ON Table1.Field3 = Table2.Field4ID
GROUP BY Table1.Field3, Table2.Field4ID, Table2.Field5
ORDER BY First(Table1.Field1);

produced exactly the results you wanted when either Field3 or Field4
were indexed. When they weren't it produced a different set of data
that still satisfied the criteria. Without the index, Field4 comes up
in an almost random fashion causing a different record in Table1 to
come up as the first record. Note: I have not tested this query on
anything beyond the sample data you show. Also, Squirrel's proposed
table structure looks nice.

James A. Fortune
Nov 13 '05 #3

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

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
1
by: Carl B Davis | last post by:
Help please!!! I am an intermediate access user that is getting my bottom kicked by what seems an easy problem to fix. I maintain an employee database at work. I have set up a query from two tables...
8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
1
by: Ersin Gençtürk | last post by:
hi , I have 2 tables , tUser and tUserRole and I have a query like : select * from tUser inner join tUserRole on tUserRole.UserId=tUser.UserId where tUser.UserId=1 this will return a user...
9
by: cj | last post by:
I'm trying to forge ahead with Visual Basic .Net but recently I've suffered several major set backs in demonstrating VB is the future and we should move from Visual FoxPro. I really need to find...
3
by: loosecannon_1 | last post by:
I get a 90-120 second blocking when send 15 or so simultaneous queries to SQL Server 2000 that query a view made up of two joined tables. After each query is blocking for the same amount of time...
5
by: zMisc | last post by:
Are there any tricks in updaitng a JOINed recordset? I joned to tables and when I try to change a field on the recordset and update it, I get this error: "Unknown column 'CCDE' in 'where...
0
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.