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

Can't find good SQL query

PL
Hi,
I have 2 simple tables, 'Accesses' and 'Partners'.

Partners :
- Id, primary numeric key
- Name of the partner
- other informational fields

Accesses :
- Id, primary numeric key
- some informational fields
- 'CreatedBy', which is a partner Id
- 'SupportBy', which is also a partner Id
I want to display a table of all accesses, with for each access the name of
the creator partner and the name of the support partner.
How can I do that in a single SQL query ?

for example :
Partners :
1 Bob
2 Bruce

Accesses :
1 Access1 CreatedBy=1 SupportBy=2
1 Access2 CreatedBy=1 SupportBy=1

Expected display :

Access1, created by Bob, supported by Bruce
Access2, created by Bob, supported by Bob

thanks
P.
Aug 14 '06 #1
2 1618
Firstly, create the relations between the 3 tables if you have not already
done so.
1. Choose Relationships on the Tools menu.

2. Add the Accesses table and 2 copies of the Partners table.
Access will alias the 2nd one as Partners_1.

3. Drag Partners.Id onto Accesses.CreatedBy.
Check the box for Referential integrity, and create.

4. Drag Partners.Id onto Accesses.SupportBy.
Check the box for Referential integrity, and create.

5. Close the Relationships window, saving the view.

Now you can do the same thing in the query, using 2 copies of the Partners
table.

1. Create the query with Partners and Accesses.
Access will show 2 lines joining the tables.
Delete the line to Accesses.SupportBy.

2. Double-click the remaining join line.
Access offers a dialog with 3 options.
Choose the one that says:
"All records from Accesses, and any matches from Partners."
This is called an outer join.

3. Add a 2nd copy of Partners to the query.
This time, add a join from Partners_1.ID to Accesses.SupportBy.

4. Double-click this join line, and make it an outer join as before.

5. Now you can now add the fields from all tables to the query, to get the
names and output you desire.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"PL" <no****@nomail.comwrote in message
news:44***********************@news.free.fr...
>
I have 2 simple tables, 'Accesses' and 'Partners'.

Partners :
- Id, primary numeric key
- Name of the partner
- other informational fields

Accesses :
- Id, primary numeric key
- some informational fields
- 'CreatedBy', which is a partner Id
- 'SupportBy', which is also a partner Id
I want to display a table of all accesses, with for each access the name
of
the creator partner and the name of the support partner.
How can I do that in a single SQL query ?

for example :
Partners :
1 Bob
2 Bruce

Accesses :
1 Access1 CreatedBy=1 SupportBy=2
1 Access2 CreatedBy=1 SupportBy=1

Expected display :

Access1, created by Bob, supported by Bruce
Access2, created by Bob, supported by Bob

Aug 14 '06 #2
PL
Great!
thanks a lot Allen.
P.

"Allen Browne" <Al*********@SeeSig.Invalida écrit dans le message de
news:44***********************@per-qv1-newsreader-01.iinet.net.au...
Firstly, create the relations between the 3 tables if you have not already
done so.
1. Choose Relationships on the Tools menu.

2. Add the Accesses table and 2 copies of the Partners table.
Access will alias the 2nd one as Partners_1.

3. Drag Partners.Id onto Accesses.CreatedBy.
Check the box for Referential integrity, and create.

4. Drag Partners.Id onto Accesses.SupportBy.
Check the box for Referential integrity, and create.

5. Close the Relationships window, saving the view.

Now you can do the same thing in the query, using 2 copies of the Partners
table.

1. Create the query with Partners and Accesses.
Access will show 2 lines joining the tables.
Delete the line to Accesses.SupportBy.

2. Double-click the remaining join line.
Access offers a dialog with 3 options.
Choose the one that says:
"All records from Accesses, and any matches from Partners."
This is called an outer join.

3. Add a 2nd copy of Partners to the query.
This time, add a join from Partners_1.ID to Accesses.SupportBy.

4. Double-click this join line, and make it an outer join as before.

5. Now you can now add the fields from all tables to the query, to get the
names and output you desire.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"PL" <no****@nomail.comwrote in message
news:44***********************@news.free.fr...

I have 2 simple tables, 'Accesses' and 'Partners'.

Partners :
- Id, primary numeric key
- Name of the partner
- other informational fields

Accesses :
- Id, primary numeric key
- some informational fields
- 'CreatedBy', which is a partner Id
- 'SupportBy', which is also a partner Id
I want to display a table of all accesses, with for each access the name
of
the creator partner and the name of the support partner.
How can I do that in a single SQL query ?

for example :
Partners :
1 Bob
2 Bruce

Accesses :
1 Access1 CreatedBy=1 SupportBy=2
1 Access2 CreatedBy=1 SupportBy=1

Expected display :

Access1, created by Bob, supported by Bruce
Access2, created by Bob, supported by Bob


Aug 14 '06 #3

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

Similar topics

1
by: Jim Geissman | last post by:
Help, please. I am trying to update a table with this structre: CREATE TABLE Queue (PropID int, EffDate smalldatetime, TxnAmt int) INSERT Queue (PropID) SELECT 1 INSERT Queue (PropID)...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
11
by: Randell D. | last post by:
Folks, I have a table of addresses and a seperate table with contact names - All addresses tie to one or more names - I would like to keep track of the number of names 'belonging' to an address...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
1
by: I_was_here | last post by:
I have a LOT of tables and queries and a few of them are not linked to a form or a report and do not have relationships. Those few tables/queries are probably junk and I want to throw them out. ...
9
by: Mike Bridge | last post by:
I am using MS Access 2003, and I typed in a query in SQL view which was working fine. Now when I open it, it gives me the error "Join expression not supported". Apparently, Access "fixed" it for...
9
by: Fish Womper | last post by:
I am at best a part time developer of Access databases. I use Access 2.0, as this is all my employer has on its computers. Even so, to use this ancient version requires a fairly convoluted...
13
by: forbes | last post by:
Hi, I have a user that used the Query Wizard to create a query in Access. Now she claims that her master table is missing all the data that was excluded from the query. Can you create anything...
2
by: blackduck603 | last post by:
Hi, I am fairly new to Access programming and Reports. I have inherited a project and am trying to understand some of the inner workings. There is a field named Overall in the Detail section of my...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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...

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.