SQL How to Select the most recent date item

后端 未结 6 1210
花落未央
花落未央 2020-12-14 15:49

Hello I have a table with columns:

*using oracle

ID                  NUMBER
USER_ID            NUMBER
DATE_ADDED          DATE
DATE_VIEWED        DAT         


        
6条回答
  •  既然无缘
    2020-12-14 16:19

    You haven't specified what the query should return if more than one document is added at the same time, so this query assumes you want all of them returned:

    SELECT t.ID,
           t.USER_ID,
           t.DATE_ADDED,
           t.DATE_VIEWED,
           t.DOCUMENT_ID,
           t.URL,
           t.DOCUMENT_TITLE,
           t.DOCUMENT_DATE
    FROM (
      SELECT test_table.*,
             RANK()
             OVER (ORDER BY DOCUMENT_DATE DESC) AS the_rank
      FROM   test_table
      WHERE  user_id = value
      )
    WHERE the_rank = 1;
    

    This query will only make one pass through the data.

提交回复
热议问题