473,399 Members | 2,146 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,399 software developers and data experts.

How do you apply PK filters to FK tables?

JRBower
14
In my main users table I filter job apps to one of four regions. Here's the code for one region:

Expand|Select|Wrap|Line Numbers
  1. SELECT    Users.UserID, Users.FirstName, Users.LastName, Users.Age, Nationalities.Nationality, Genders.Gender, NativeLanguages.NativeLanguage, Users.City, States.State, Countries.Country, Users.ApplicationDate
  2. FROM      dbo.Users
  3. left join dbo.Nationalities on dbo.Users.NationalityID = dbo.Nationalities.NationalityID
  4. left join dbo.Genders on dbo.Users.GenderID = dbo.Genders.GenderID
  5. left join dbo.NativeLanguages on dbo.Users.NativeLanguageID = dbo.NativeLanguages.NativeLanguageID
  6. left join dbo.States on dbo.Users.StateID = dbo.States.StateID
  7. left join dbo.Countries on dbo.Users.CountryID = dbo.Countries.CountryID
  8. WHERE      dbo.Users.AccessID = 2
  9.   AND        dbo.Countries.RegionFilter = 'Americas'
  10. ORDER BY  ApplicationDate DESC
There are several other tables joined to the Users table that need the same region filter applied to as well. Here's the EmergencyContact table:

Expand|Select|Wrap|Line Numbers
  1. SELECT    Contacts.ContactID, Contacts.UserID, Contacts.ContactFirstName, Contacts.ContactLastName, UserContacts.Contact, Contacts.City, States.State, Countries.Country, Contacts.Phone,
  2. (Users.FirstName) + ' ' + (Users.LastName) AS TheUser, Users.FirstName, Users.LastName
  3. FROM      dbo.Contacts
  4. left join dbo.Users on dbo.Users.UserID = dbo.Contacts.UserID
  5. left join dbo.UserContacts on dbo.Contacts.UserContactID = dbo.UserContacts.UserContactID
  6. left join dbo.States on dbo.Contacts.StateID = dbo.States.StateID
  7. left join dbo.Countries on dbo.Contacts.CountryID = dbo.Countries.CountryID
I tried adding:

Expand|Select|Wrap|Line Numbers
  1. WHERE Contacts.UserID = Users.UserID 
  2.     AND Users.CountryID = Countries.CountryID
  3.     AND Countries.RegionFilter = 'Americas'
But it doesn't work. Any ideas how I can apply the PK filter to the FK?

Thanks,
JRBower
Mar 15 '08 #1
5 1504
ck9663
2,878 Expert 2GB
try:

Expand|Select|Wrap|Line Numbers
  1. SELECT    Contacts.ContactID, Contacts.UserID, Contacts.ContactFirstName, Contacts.ContactLastName, UserContacts.Contact, Contacts.City, States.State, Countries.Country, Contacts.Phone,
  2. (Users.FirstName) + ' ' + (Users.LastName) AS TheUser, Users.FirstName, Users.LastName
  3. FROM      dbo.Contacts
  4. left join dbo.Users on dbo.Users.UserID = dbo.Contacts.UserID
  5. left join dbo.UserContacts on dbo.Contacts.UserContactID = dbo.UserContacts.UserContactID
  6. left join dbo.States on dbo.Contacts.StateID = dbo.States.StateID
  7. left join dbo.Countries on dbo.Contacts.CountryID = dbo.Countries.CountryID and  Countries.RegionFilter = 'Americas'
  8.  
-- CK
Mar 17 '08 #2
JRBower
14
Hi CK,
Thanks again for your time. Perhaps I should have used a different table for my example. The suggestion you gave works as I would have expected, however, the contacts table is a special case where I need to sort on the country that is associated with the UserID for the Users table (dbo.Users on dbo.Users.UserID = dbo.Contacts.UserID) and not the ContactID (Contacts.ContactID) of the UserContact table. The reason in this case is that a contact might live in a different country than the user (e.g., I (UserID) live in America but my family (ContactID) live in the Canada.

The Users table has a one to many relationship to the Contacts table, I also enforce referential integrity including using cascade updates and cascade deletes of related records.

What I wanted to achieve in my original post was to be able to filter a table like the Certificates table via the FK based on the PK of the users table. So if the Users table filter (dbo.Countries.RegionFilter = 'Americas') sends my application to the "Americas" region for processing I aslo want the related records within the Contacts table and Certificates table to be filtered to the 'Americas' also.

In the following table users can enter the type of training certificates they have. So I'm wondering how I can filter this (via the FK) so any related table records go where I want them to go.

Expand|Select|Wrap|Line Numbers
  1. SELECT    CertificateID, Certificates.UserID, Certificate, Institution, CertificationDate, (Users.FirstName) + ' ' + (Users.LastName) AS TheUser, Users.FirstName, Users.LastName
  2. FROM      dbo.Certificates
  3. left join dbo.Users on dbo.Certificates.UserID = dbo.Users.UserID
Hopefully I communicated it better this time. :)

Regards,
James
Mar 17 '08 #3
JRBower
14
CK,
As a follow up to my initial enquiry I should say that my Countries table is basically layed out like this:

Expand|Select|Wrap|Line Numbers
  1. :::CountryID || Country  || RegionFilter
  2. __1 _________|| America ||   Americas
  3. __2_________ || Canada ||   Americas
  4. __3_________ || England ||   Europa
  5. __4________._|| France. ||   Europa
etc

And of course I can filter FK tables like this:

Expand|Select|Wrap|Line Numbers
  1. WHERE   dbo.Users.CountryID IN ('1', '2', 'etc')
So I can do this:

Expand|Select|Wrap|Line Numbers
  1. SELECT    CertificateID, Certificates.UserID, Certificate, Institution, CertificationDate, (Users.FirstName) + ' ' + (Users.LastName) AS TheUser, Users.FirstName, Users.LastName
  2. FROM      dbo.Certificates
  3. left join dbo.Users on dbo.Certificates.UserID = dbo.Users.UserID
  4. WHERE   dbo.Users.CountryID IN ('1', '2', 'etc')
which works fine but, unfortunately, it requires that I list up to 70 CountryID's for one region instead of the simpler:

Expand|Select|Wrap|Line Numbers
  1. WHERE   dbo.Countries.RegionFilter = 'Americas'
I'm just not sure how to write the SQL that would make dbo.Countries.RegionFilter = 'Americas' possible in queries in which dbo.Users.CountryID IN ('1', '2', 'etc') has no problem.

James
Mar 18 '08 #4
ck9663
2,878 Expert 2GB
CK,
As a follow up to my initial enquiry I should say that my Countries table is basically layed out like this:

Expand|Select|Wrap|Line Numbers
  1. :::CountryID || Country  || RegionFilter
  2. __1 _________|| America ||   Americas
  3. __2_________ || Canada ||   Americas
  4. __3_________ || England ||   Europa
  5. __4________._|| France. ||   Europa
etc

And of course I can filter FK tables like this:

Expand|Select|Wrap|Line Numbers
  1. WHERE   dbo.Users.CountryID IN ('1', '2', 'etc')
So I can do this:

Expand|Select|Wrap|Line Numbers
  1. SELECT    CertificateID, Certificates.UserID, Certificate, Institution, CertificationDate, (Users.FirstName) + ' ' + (Users.LastName) AS TheUser, Users.FirstName, Users.LastName
  2. FROM      dbo.Certificates
  3. left join dbo.Users on dbo.Certificates.UserID = dbo.Users.UserID
  4. WHERE   dbo.Users.CountryID IN ('1', '2', 'etc')
which works fine but, unfortunately, it requires that I list up to 70 CountryID's for one region instead of the simpler:

Expand|Select|Wrap|Line Numbers
  1. WHERE   dbo.Countries.RegionFilter = 'Americas'
I'm just not sure how to write the SQL that would make dbo.Countries.RegionFilter = 'Americas' possible in queries in which dbo.Users.CountryID IN ('1', '2', 'etc') has no problem.

James
Let's follow your code:


SELECT CertificateID, Certificates.UserID, Certificate, Institution, CertificationDate, (Users.FirstName) + ' ' + (Users.LastName) AS TheUser, Users.FirstName, Users.LastName
FROM dbo.Certificates
left join dbo.Users on dbo.Certificates.UserID = dbo.Users.UserID
WHERE dbo.Users.CountryID IN (select countryid from CountriesTable where RegionFilter = 'Americas')

Happy Coding.

-- CK
Mar 19 '08 #5
JRBower
14
A subquery! of course. :)

Thanks!

Let's follow your code:


SELECT CertificateID, Certificates.UserID, Certificate, Institution, CertificationDate, (Users.FirstName) + ' ' + (Users.LastName) AS TheUser, Users.FirstName, Users.LastName
FROM dbo.Certificates
left join dbo.Users on dbo.Certificates.UserID = dbo.Users.UserID
WHERE dbo.Users.CountryID IN (select countryid from CountriesTable where RegionFilter = 'Americas')

Happy Coding.

-- CK
Mar 19 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: esmith2112 | last post by:
Having a bear of a time trying to implement replication under 8.2 environment. I've created all of the control structures on both source and target database and can actually see data being staged...
0
by: mdb_1974 | last post by:
Hello I tried to do an initial full refresh but I failed - nothing happens (no error at all). Apply works without errors. Details of my environment: I have the following replication scenario:...
6
by: TJO | last post by:
Below is some sample code that fades div tags that is not working in IE 6.0.29 on xp sp2. Can anyone help see why the if(ie5) document.getElementById(divID).filters.alpha.opacity lines are not...
1
by: Dieter Vanderelst | last post by:
Hello, I'm trying to access the Filters-Dll provided by the filters-project (http://filters.sourceforge.net/index.htm). Following the advice I got from the Python list -thank you for that-, I...
6
by: buntyindia | last post by:
{"length":50,"accounting":} I am rendering the above JSON object in a tabel using DOM . Now i want to add a functionality of Filters means There is some check box specifying age if I...
1
by: katie.liddle | last post by:
Hi guys, I have written the following code to filter based on a selection from a dropdown box, and it works: Private Sub txt_FilterByWhat_Change() DoCmd.ShowAllRecords If (Me.FilterByWhere =...
1
by: eHaak | last post by:
A couple years ago, I built a database in MS Access 2003. I built the form using macros in some of the command buttons, and now I’m trying to eliminate the macros and just use visual basic code. ...
1
by: kkshansid | last post by:
i hav 2 tables named student and school student fields stdid,stdname,schoolcode,result school fields code,name,state i have to make join of both tables on schoolcode=code but some school code...
10
by: edgalljr | last post by:
I have a Button on a form that filters on 2 fields using the following code: Private Sub cmd_NavyDue_Click() With Me .FilterOn = True DoCmd.ApplyFilter , " = 'NAVY' And >1" Me.OrderBy =...
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...
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
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...
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...

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.