Results 1 to 11 of 11
  1. #1
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182

    Complex Query and Compound Primary Key

    Hello.



    I am having a problem with a query. Basically, I have two tables. One table, lets call it tblDate, supplies me with the [Date] and a [CaseID]. [CaseID] is the primary key for tblDate.

    I have another table called tblMain. tblMain is comprised of [CaseID], [Characteristic], [City1], [City2], and [City3]. [CaseID] and [Characteristic] are a combined primary key. The values for [Characteristic] can be "Alpha", "Beta", or "Gamma".

    [tblDate].[CaseID] = [tblMain].[CaseID].

    Okay, so that is the setup of my tables.

    I am trying to make a query that will show me how often a city is linked to a case. I do not care if it is [City1] [City2] or [City3]. Additionally,

    "Alpha" also supercedes "Beta" and "Gamma." For example, if I have CASEID#33 that is characterised as BOTH "Alpha" and "Beta", I will have two records with [CaseID] = #33. When I run my query, I want to ignore EVERYTHING having to do with the record that contains "Beta".

    I am having difficulty with this query however.

  2. #2
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    I have made a query, using "Total-First" for [City 1] [City2] and [City3] and "Total-Min" for [Characteristic]. I then enter criteria "New York" under each city. Using OR (so its not on the same line, but different lines).

    However, One incident is set up as such on tblMain
    CaseID 55 Characteristic=Alpha City1 = Seattle City2= Hong Kong City3=Richmond
    CaseID 55 Characteristic=Beta City1 = Bankog City2= New York City3=London

    The problem is that my query returns CaseID55 WHEN IT SHOULD NOT. The query replaces "City2 = HongKong" with "City2 = New York". But since "New York" is characterized under "Beta" it should be ignored completed due to the "Total-MinOfCharacteristic".

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    you cant join the 2 tables, and under the field set criteria: <> "beta" ?

  4. #4
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    There is a lot more information in those tables. No. I am not able to make design changes to them.

  5. #5
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    First, get the records that you want before setting criteria:
    - query 1, all records with Alpha
    - query 2, all records on query 1 plus records on tblMain with Beta that don't exist on query 1
    - query 3, all records on query 2 plus records on tblMain with Gamma that don't exist on query 2

    Use query 3 as your main list of records to begin selecting the city.

  6. #6
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Thank you aytee111. That seems simple enough. I will give that a try.

    On Query2, would I enter "Where [Query1].[Characteristic] <> Beta" and [tblMain] = Beta?

  7. #7
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Not so simple after all!

    Query1 - Alpha:
    SELECT tblMain.CaseID, tblMain.Characteristic FROM tblMain WHERE (((tblMain.Characteristic)="alpha"));

    Query 2 - Beta:
    SELECT tblMain.CaseID, tblMain.Characteristic FROM tblMain LEFT JOIN Query1 ON tblMain.CaseID = Query1.CaseID WHERE (((tblMain.Characteristic)="beta") AND ((Query1.CaseID) Is Null));

    Query 3 - Gamma:
    SELECT tblMain.CaseID, tblMain.Characteristic FROM (tblMain LEFT JOIN Query1 ON tblMain.CaseID = Query1.CaseID) LEFT JOIN Query2 ON tblMain.CaseID = Query2.CaseID WHERE (((tblMain.Characteristic)="gamma") AND ((Query1.CaseID) Is Null) AND ((Query2.CaseID) Is Null));

    Query 4 - the full list, one record per CaseID:
    SELECT * FROM Query1
    UNION SELECT * FROM Query2
    UNION SELECT * FROM query3;

    Now use query 4 to check for cities.

  8. #8
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Thanks. I added a "Date" criteria to Query1. I want to pull only data from 2016. This query works correctly.

    But for some reason, Query2 is ignoring the fact that there is a Date parameter in Query1.

  9. #9
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Query1 are Alpha records, query2 are Beta records - where does the date come in for Beta?

  10. #10
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    I have alpha, beta, and gamma records going back to 1992. I only want to look at 2016 for all three characteristics

  11. #11
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Each time you read the tblMain to select records you will need to add the date criteria.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Compound IF trouble #error
    By wcrimi in forum Programming
    Replies: 7
    Last Post: 11-21-2015, 03:36 PM
  2. Compound in a DLookUp function
    By Lou_Reed in forum Access
    Replies: 7
    Last Post: 05-27-2015, 11:06 AM
  3. Trouble with Compound Query
    By bxdobs in forum Queries
    Replies: 3
    Last Post: 02-20-2015, 11:42 AM
  4. Replies: 4
    Last Post: 12-18-2011, 05:55 PM
  5. Creating Compound summations etc.
    By ohthesilhouettes in forum Queries
    Replies: 2
    Last Post: 06-19-2011, 12:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums