MAX(DATE) - SQL ORACLE

前端 未结 5 2075
感动是毒
感动是毒 2021-02-20 03:03

I want to select only the latest membership_id from table user_payments of the user with the user_id equal to 1.

This is how the table user_payment looks like:



        
5条回答
  •  礼貌的吻别
    2021-02-20 03:12

    Oracle 9i+ (maybe 8i too) has FIRST/LAST aggregate functions, that make computation over groups of rows according to row's rank in group. Assuming all rows as one group, you'll get what you want without subqueries:

    SELECT
      max(MEMBSHIP_ID)
      keep (
          dense_rank first
          order by paym_date desc NULLS LAST
      ) as LATEST_MEMBER_ID
    FROM user_payment
    WHERE user_id=1
    

提交回复
热议问题