How do you apply PK filters to FK tables?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JRBower
    New Member
    • Sep 2007
    • 14

    How do you apply PK filters to FK tables?

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

    Code:
    SELECT    Users.UserID, Users.FirstName, Users.LastName, Users.Age, Nationalities.Nationality, Genders.Gender, NativeLanguages.NativeLanguage, Users.City, States.State, Countries.Country, Users.ApplicationDate
    FROM      dbo.Users
    left join dbo.Nationalities on dbo.Users.NationalityID = dbo.Nationalities.NationalityID
    left join dbo.Genders on dbo.Users.GenderID = dbo.Genders.GenderID
    left join dbo.NativeLanguages on dbo.Users.NativeLanguageID = dbo.NativeLanguages.NativeLanguageID
    left join dbo.States on dbo.Users.StateID = dbo.States.StateID
    left join dbo.Countries on dbo.Users.CountryID = dbo.Countries.CountryID
    WHERE	  dbo.Users.AccessID = 2
      [B]AND  	  dbo.Countries.RegionFilter = 'Americas'[/B]
    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 EmergencyContac t table:

    Code:
    SELECT    Contacts.ContactID, Contacts.UserID, Contacts.ContactFirstName, Contacts.ContactLastName, UserContacts.Contact, Contacts.City, States.State, Countries.Country, Contacts.Phone,
    (Users.FirstName) + ' ' + (Users.LastName) AS TheUser, Users.FirstName, Users.LastName
    FROM      dbo.Contacts
    left join dbo.Users on dbo.Users.UserID = dbo.Contacts.UserID
    left join dbo.UserContacts on dbo.Contacts.UserContactID = dbo.UserContacts.UserContactID
    left join dbo.States on dbo.Contacts.StateID = dbo.States.StateID
    left join dbo.Countries on dbo.Contacts.CountryID = dbo.Countries.CountryID
    I tried adding:

    Code:
    WHERE Contacts.UserID = Users.UserID 
    	AND Users.CountryID = Countries.CountryID
    	AND Countries.RegionFilter = 'Americas'
    But it doesn't work. Any ideas how I can apply the PK filter to the FK?

    Thanks,
    JRBower
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    try:

    Code:
    SELECT    Contacts.ContactID, Contacts.UserID, Contacts.ContactFirstName, Contacts.ContactLastName, UserContacts.Contact, Contacts.City, States.State, Countries.Country, Contacts.Phone,
    (Users.FirstName) + ' ' + (Users.LastName) AS TheUser, Users.FirstName, Users.LastName
    FROM      dbo.Contacts
    left join dbo.Users on dbo.Users.UserID = dbo.Contacts.UserID
    left join dbo.UserContacts on dbo.Contacts.UserContactID = dbo.UserContacts.UserContactID
    left join dbo.States on dbo.Contacts.StateID = dbo.States.StateID
    left join dbo.Countries on dbo.Contacts.CountryID = dbo.Countries.CountryID and  Countries.RegionFilter = 'Americas'
    -- CK

    Comment

    • JRBower
      New Member
      • Sep 2007
      • 14

      #3
      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.UserI D = dbo.Contacts.Us erID) and not the ContactID (Contacts.Conta ctID) 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.

      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
      Hopefully I communicated it better this time. :)

      Regards,
      James

      Comment

      • JRBower
        New Member
        • Sep 2007
        • 14

        #4
        CK,
        As a follow up to my initial enquiry I should say that my Countries table is basically layed out like this:

        Code:
        [U]:::CountryID || Country  || RegionFilter[/U]
        [U]__1 _________|| America ||   Americas[/U]
        [U]__2_________ || Canada ||   Americas[/U]
        [U]__3_________ || England ||   Europa[/U]
        [U]__4________._|| France. ||   Europa[/U]
        etc

        And of course I can filter FK tables like this:

        Code:
        WHERE   dbo.Users.CountryID IN ('1', '2', 'etc')
        So I can do this:

        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
        [B]WHERE   dbo.Users.CountryID IN ('1', '2', 'etc')[/B]
        which works fine but, unfortunately, it requires that I list up to 70 CountryID's for one region instead of the simpler:

        Code:
        WHERE   dbo.Countries.RegionFilter = 'Americas'
        I'm just not sure how to write the SQL that would make dbo.Countries.R egionFilter = 'Americas' possible in queries in which dbo.Users.Count ryID IN ('1', '2', 'etc') has no problem.

        James

        Comment

        • ck9663
          Recognized Expert Specialist
          • Jun 2007
          • 2878

          #5
          Originally posted by JRBower
          CK,
          As a follow up to my initial enquiry I should say that my Countries table is basically layed out like this:

          Code:
          [U]:::CountryID || Country  || RegionFilter[/U]
          [U]__1 _________|| America ||   Americas[/U]
          [U]__2_________ || Canada ||   Americas[/U]
          [U]__3_________ || England ||   Europa[/U]
          [U]__4________._|| France. ||   Europa[/U]
          etc

          And of course I can filter FK tables like this:

          Code:
          WHERE   dbo.Users.CountryID IN ('1', '2', 'etc')
          So I can do this:

          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
          [B]WHERE   dbo.Users.CountryID IN ('1', '2', 'etc')[/B]
          which works fine but, unfortunately, it requires that I list up to 70 CountryID's for one region instead of the simpler:

          Code:
          WHERE   dbo.Countries.RegionFilter = 'Americas'
          I'm just not sure how to write the SQL that would make dbo.Countries.R egionFilter = 'Americas' possible in queries in which dbo.Users.Count ryID IN ('1', '2', 'etc') has no problem.

          James
          Let's follow your code:


          SELECT CertificateID, Certificates.Us erID, Certificate, Institution, CertificationDa te, (Users.FirstNam e) + ' ' + (Users.LastName ) AS TheUser, Users.FirstName , Users.LastName
          FROM dbo.Certificate s
          left join dbo.Users on dbo.Certificate s.UserID = dbo.Users.UserI D
          WHERE dbo.Users.Count ryID IN (select countryid from CountriesTable where RegionFilter = 'Americas')

          Happy Coding.

          -- CK

          Comment

          • JRBower
            New Member
            • Sep 2007
            • 14

            #6
            A subquery! of course. :)

            Thanks!

            Originally posted by ck9663
            Let's follow your code:


            SELECT CertificateID, Certificates.Us erID, Certificate, Institution, CertificationDa te, (Users.FirstNam e) + ' ' + (Users.LastName ) AS TheUser, Users.FirstName , Users.LastName
            FROM dbo.Certificate s
            left join dbo.Users on dbo.Certificate s.UserID = dbo.Users.UserI D
            WHERE dbo.Users.Count ryID IN (select countryid from CountriesTable where RegionFilter = 'Americas')

            Happy Coding.

            -- CK

            Comment

            Working...