【问题标题】:how to get latest 'n' updated row from a table [duplicate]如何从表中获取最新的'n'更新行[重复]
【发布时间】:2012-10-18 16:12:14
【问题描述】:

可能重复:
Oracle SQL - How to Retrieve highest 5 values of a column

我有一个表 abc,其中我有以下列

act_id,cust_id,lastUpdatedDate,custActivityact_id 是主键

lastUpdatedDate 存储为此客户完成的最后一项活动。

我正在尝试根据 lastUpdatedDate 获取给定 custid 的最新 10 行。

我怎样才能实现它。

-vivek

【问题讨论】:

    标签: sql oracle oracle10g top-n


    【解决方案1】:

    您可以在 Oracle 中使用 ROWNUM。点击Here 获取文档

    select *
    from  
       ( select * 
         from your_table 
         where cust_id=<given cust_id>
         order by lastUpdatedDate desc ) 
    where ROWNUM <= 10;
    

    【讨论】:

      【解决方案2】:

      Oracle 支持 ROW_NUMBER() 和窗口函数。试试下面的,

      SELECT act_id, cust_id, lastUpdatedDate, custActivity
      FROM
      (
          SELECT  act_id, cust_id, lastUpdatedDate, custActivity,
                  ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY lastUpdatedDate DESC) rn
          FROM tableNAME
      ) x
      WHERE rn <= 10
      

      【讨论】:

        【解决方案3】:

        希望这对你有帮助-

        select act_id,cust_id,lastUpdatedDate,custActivity
          from (
            select act_id,cust_id,lastUpdatedDate,custActivity, row_number() over (order by lastUpdatedDate) r
              from abc
            where act_id=<cust_id>
          )
        where r between 1 and 10;
        

        【讨论】:

          猜你喜欢
          • 2022-01-22
          • 2021-01-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-19
          • 2021-01-31
          • 1970-01-01
          相关资源
          最近更新 更多