从sqlplus查询中选择顶行

时间:2014-04-26 08:42:51

标签: sql database plsql sqlplus oracle-xe

我有一个查询

select sum(pur.purchase_net_invoice_value), par.party_desc  from purchase pur
join party par
on par.party_id = pur.party_id
group by par.party_desc;

运行良好。

我只想查询前五行并弃掉其他查询。

2 个答案:

答案 0 :(得分:1)

您可以使用ROWNUM Pseudocolumn 来限制行数 在子查询中添加ORDER BY子句以定义要显示的行。

SELECT *
FROM (
    select sum(pur.purchase_net_invoice_value), par.party_desc
    from purchase pur
    join party par on par.party_id = pur.party_id
    group by par.party_desc
)
WHERE ROWNUM <= 5;

答案 1 :(得分:1)

您可以使用rownum pseduocolumn:

SELECT * 
FROM (SELECT   SUM(pur.purchase_net_invoice_value), par.party_desc
      FROM     purchase pur
      JOIN     party par ON par.party_id = pur.party_id
      GROUP BY par.party_desc
      ORDER BY 1 DESC)
WHERE rownum <= 5;