Results 1 to 10 of 10
  1. #1
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31

    Query first record of each unique foreign key

    Hello,



    I want to query a table.

    its a payments table that the Id field is PaymentId and also have a foreign key CustomerId and more fields.

    the thing is that I want to see in the results every customerId only one time (even if he is there alot of times), actually show me the first record of each unique customerId is in the table.

    thank you.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    make 2 queries....

    Q1:to get the eariest ID using MIN:
    select custID, Min(PayID) from tPayments

    then in Q2, get all the data
    Q2:
    select tCustomers.* from Q1, tCustomers where tCustomers.custID = Q1.custID

  3. #3
    Join Date
    Apr 2017
    Posts
    1,792
    How you determine the 1st occurrence of key? Access tables (and any relational databases tables) don't have any internal row position. You must have some parameter to decide, which entry is earlier and which later one. P.e. when PaymentID is autonumeric (and those autonumeric PaymentID's were never recreated anew), then the condition can be that you search smallest PaymentID value for every CustomerID. Or when you have PaymentDate field in table, and there are no duplicate payments on same day, then the condition is smallest PaymentDate for every CustomerID.

  4. #4
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31
    Thank you very much but I was wrong.

    I have one more important field "PayDate", instead of get the first record of each unique customerId I need the "Min" of "PayDate" field of each unique customerId.

  5. #5
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31
    someone ??

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Can you not alter what was posted in #2 to suit your new requirements? If not, maybe if you posted some data and revealed the exact names of your tables/fields. This is probably also possible using a subquery, but without knowing those names, it's too difficult to post meaningful/accurate sql statements. Or you could look at here at TOP N Records per Group and use the Min of your date field instead. In that case, you wouldn't use IN as shown in the example, but more likely =.

    http://allenbrowne.com/subquery-01.html
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31
    Ok, here is the table filelds and my needs:

    table name:tbl_Payments

    Fields: PaymentId (Master Key), CustomerId, ProductSaleId, Cost, Price, PayDate, Notes

    My needs:
    Query that shows every customerId only one time with the earliest date in the PayDate field, and if there is several of the same PayDate that is the earliest, show only the one with the smallest PaymentId, the main rule that in the end every customerId will have one record.

    I hope I was more clear now.

    Thank you !

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Did you forget to include some data in your last post?

  9. #9
    nd0911 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    31
    No I didn't but I can.

    PaymentId , CustomerId, ProductSaleId, Cost, Price, PayDate, Notes
    1 , 1 , 1 , 100, 300 , 1/1/18, bla bla
    2 , 1 , 2 , 110, 320 , 1/1/18,
    3 , 2 , 3 , 80 , 200 , 3/1/18,
    4 , 1 , 1 , 100, 300 , 1/2/18,
    5 , 2 , 3 , 110, 320 , 1/2/18, bla bla 2

  10. #10
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Well, it seems that things have changed. From post 4:
    instead of get the first record of each unique customerId I need the "Min" of "PayDate"
    Now it's
    the earliest date in the PayDate field, and if there is several of the same PayDate that is the earliest, show only the one with the smallest PaymentId,
    As far as I know, a subquery can "filter" a group of records based on one field only and you seem to now be saying 'it's field A unless condition1, then it's field B. I don't think that is possible, because what a subquery returns is kind of "linked" to the outer query on the same field, thus doesn't allow for flipping between field A or field B. At least I wouldn't know how to do that.

    You never commented on your ability to adapt what was given in post 2. Seems like you should try that.

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

Similar Threads

  1. Add New Record With Same Foreign Key
    By Homegrownandy in forum Forms
    Replies: 5
    Last Post: 07-21-2015, 06:33 AM
  2. Foreign Key/Unique ID Problem
    By Raptor_45 in forum Programming
    Replies: 9
    Last Post: 02-02-2013, 11:25 PM
  3. My first access database - new foreign key record
    By michal_milena in forum Access
    Replies: 1
    Last Post: 10-21-2012, 03:30 PM
  4. Replies: 10
    Last Post: 05-08-2012, 09:17 AM
  5. Replies: 2
    Last Post: 04-19-2012, 11:29 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