Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2017
    Posts
    1

    Unanswered: 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
    Join Date
    Oct 2007
    Posts
    161
    Provided Answers: 9
    Code:
    select columns_you_want
       from your_table a
    where ? = ?
      and orderdate = (select max(orderdate)
                                   from your_table b
                                where a.userid = b.userid)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •