Results 1 to 4 of 4
  1. #1
    gicarto is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Madison, Wisconsin
    Posts
    11

    Selecting Primary Key by Picking the Highest Value-No Aggregate

    Hello-



    This is my first post on this site and I don't really know how I would know what to look for in the existing forums so I apologize in advance. I am trying to write a query that will pick a record based on the highest value in a column but I am not having any luck. Here is the data I have and what I want it to look like after the query is run.

    Here is the current table I am trying to change.

    tid municipality number year value
    37201_005 Abbotsford 5 2019 15,333,800
    10201_005 Abbotsford 5 2019 586,100
    37201_006 Abbotsford 6 2019 19,004,400
    10201_006 Abbotsford 6 2019 4,771,600
    10201_007 Abbotsford 7 2019 2,094,400

    Here is how I want the final result to look like

    tid municipality number year value
    37201_005 Abbotsford 5 2019 15,333,800
    37201_006 Abbotsford 6 2019 19,004,400
    10201_007 Abbotsford 7 2019 2,094,400

    These tables are for a taxing jurisdiction in a city that is located in 2 counties. I need to pick the record (with the primary key) for the district in the County with the highest value. For example, Abbotsford has two taxing districts in two counties (numbers 5 and 6). Tax jurisdiction #5 is valued at 15,333,800 in county 1 and 586,100 in county 2. Since it is in two counties, there are two primary keys. Tax jurisdiction has the primary keys of 37201_005 and 10201_005. Since 37201_005 is valued higher than 10201_005, I want my result set to only report 37201_005. Any help will be greatly appreciated.
    Attached Thumbnails Attached Thumbnails Annotation 2020-04-27 162239.png  

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Try a TOP N query. Review http://allenbrowne.com/subquery-01.html#TopN

    Not seeing a field for county identifier.

    BTW, year and value are reserved words and should not use reserved words as names for anything.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Have a look at the attached and please let us know if that is what you wanted (look at the qryFinalMaxData).

    Cheers,
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    gicarto is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Madison, Wisconsin
    Posts
    11
    I appreciate your help. I did not try the Top N query yet. I had success using Gicu's suggestion. Here is the full query that now works in my database:

    1. Aggregate Query
    SELECT M.muniName, T.tidNumber, V.currentYear, Max(V.curVal) AS MaxOfcurVal, YP.Type
    FROM tblTifType AS YP INNER JOIN
    (tblMunicipality AS M INNER JOIN
    (tblTif AS T INNER JOIN tblTifValue AS V ON T.tid = V.tid) ON
    M.mid = T.mid) ON YP.typeID = T.tidTypeId
    GROUP BY M.muniName, T.tidNumber, V.currentYear, YP.Type, M.splitCo
    HAVING (((V.currentYear)=2019) AND ((M.splitCo)="*"));




    2. Select Record id with highest value and union with one-county district
    SELECT T.tid, M.muniName, T.tidNumber
    FROM (qselSplitTidAgg AS AG INNER JOIN
    (tblTif AS T INNER JOIN tblTifValue AS V ON T.tid = V.tid) ON
    (AG.tidNumber = T.tidNumber) AND
    (AG.currentYear = V.currentYear) AND
    (AG.MaxOfcurVal = V.curVal)) INNER JOIN
    tblMunicipality AS M ON (M.mid = T.mid) AND
    (AG.muniName = M.muniName)
    GROUP BY T.tid, M.muniName, T.tidNumber
    ORDER BY M.muniName
    UNION
    SELECT T2.tid, M2.muniName, T2.tidNumber
    FROM (tblMunicipality AS M2 INNER JOIN
    tblTif AS T2 ON M2.mid = T2.mid) INNER JOIN
    tblTifValue AS V2 ON T2.tid = V2.tid
    WHERE (((M2.splitCo) Is Null) AND
    ((T2.terminated)=No) AND
    ((V2.currentYear)=2019));

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

Similar Threads

  1. Replies: 3
    Last Post: 08-25-2016, 02:55 AM
  2. Replies: 12
    Last Post: 12-27-2015, 02:17 PM
  3. Selecting highest value across several fields
    By megabrown in forum Queries
    Replies: 19
    Last Post: 10-28-2014, 01:16 AM
  4. Replies: 10
    Last Post: 12-29-2013, 03:26 PM
  5. Replies: 3
    Last Post: 04-05-2012, 08:33 AM

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