Results 1 to 4 of 4
  1. #1
    cyberdoc is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    5

    Get last order entry for a particular user from the table


    Hi guys,
    ive a table with entries of users and the orders they made and payments details etc. now i need a query wherein only the latest orders for all users are shown and not all of them. for example,


    ORDERID | USERID | ORDERDATE | AMOUNT | PAID | BALANCE |
    ORD0001 | USR0001| 12/2/17 | 1500 | 1200 | 300 |
    ORD0002 | USR0002| 12/2/17 | 400 | 210 | 190 |
    ORD0003 | USR0002| 12/2/17 | 1000 | 800 | 200 |
    ORD0004 | USR0003| 12/2/17 | 500 | 200 | 300 |


    SO in this we would want
    only order 3 to be shown for user2 and not order2.
    Thanks in advance

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    to get the last order, you need one or more fields to indicate a sort order and therefore can identify the last record.

    So assuming orderids increment on creation then you could use

    SELECT T.*
    FROM tblOrders T INNER JOIN (SELECT UserID, max(OrderID) maxOrder FROM tblOrders GROUP BY UserID) Q ON T.UserID=Q.UserID AND T.OrderID=Q.maxOrder

    If they don't increment then you would also need to use the Orderdate field as well, but in your example all orders have the same date, so no discernible order can be made.

  3. #3
    cyberdoc is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    5
    Quote Originally Posted by Ajax View Post
    to get the last order, you need one or more fields to indicate a sort order and therefore can identify the last record.

    So assuming orderids increment on creation then you could use

    SELECT T.*
    FROM tblOrders T INNER JOIN (SELECT UserID, max(OrderID) maxOrder FROM tblOrders GROUP BY UserID) Q ON T.UserID=Q.UserID AND T.OrderID=Q.maxOrder

    If they don't increment then you would also need to use the Orderdate field as well, but in your example all orders have the same date, so no discernible order can be made.
    orderid do increment but the query gives a syntax error and doesnt work

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    since I guessed at your names, I'm not surprised if you have just copy and pasted. Wild guess is you've spelt something incorrectly or not used proper syntax.

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

Similar Threads

  1. Replies: 1
    Last Post: 09-28-2016, 05:36 PM
  2. Replies: 1
    Last Post: 11-29-2014, 12:23 PM
  3. Replies: 4
    Last Post: 04-01-2014, 02:11 PM
  4. Replies: 1
    Last Post: 12-15-2011, 04:26 AM
  5. omit word in entry for alphabetical order
    By airhud86 in forum Access
    Replies: 1
    Last Post: 12-14-2009, 03:49 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