select * from
( select t.*,row_number()over(partition by userid order by userid desc) as num
from table t)
where num<=5

解决方案 »

  1.   

    select * from
    ( select t.*,row_number()over(partition by userid order by money desc) as num
    from table t)
    where num<=5
    写错了,这个才是!
      

  2.   

    还是错了.
    select * from
    ( select t.*,row_number()over(partition by trunc(time,'yyyy') order by money desc) as num
    from table t)
    where num<=5
      

  3.   

    多谢大侠,但是你写的money应该是一个按年的汇总数,我怎么也
    写不明白,怎么把分类汇总写入到over中呢? 再指点一下。
      

  4.   


    select *, row_number()  over(partition by orderYear order by totalamount desc) as numfrom (select to_char(order_date,'yyyy') orderYear ,customer_id,sum(order_total) totalamount
    from orders a
    group by to_char(order_date,'yyyy'),customer_id
    order by totalamount desc) aawhere num<=5我先把汇总得到,再把子查询放入到from中,对其进行您给写的over中,他说未找到预期的from
      

  5.   

    select aa.*, row_number()  over(partition by aa.orderYear order by aa.totalamount desc) as numfrom (select to_char(order_date,'yyyy') orderYear ,customer_id,sum(order_total) totalamount
    from orders a
    group by to_char(order_date,'yyyy'),customer_id
    order by totalamount desc) aawhere num<=5
    试看看,我看了下,估计就是.*的问题吧,前面要指定的.
      

  6.   

    sbaz(万神渡劫)的思路是正确的,就是一个地方写错了:楼主说的未找到预期的from是因为sbaz(万神渡劫)的语句里row_number()前不能用t.*,应该列出表里具体的字段就可以执行出你要的结果了
      

  7.   

    select t1.* from customer t1,
    (select customer_id,sum(order_total) from orders group by user_id order by sum(money) desc) t2 
    where t1.customer_id=t2.customer_id
    and t2.rownum <=5
      

  8.   

    谁说不可以用T.*?
    只是他没指定而已,所以FROM找不到了.
    我已经给他加上了,不会再有问题了.
      

  9.   

    不过楼主的SQL思路还是有点问题,他应该仿我前面的写,他这么写就只能查到5条了,不是每个的5条.
      

  10.   

    select t.customer_id,sum(t.order_total) from customer t where t.rownum <=5
    group by t.customer_id order by sum(t.order_total) desc
      

  11.   

    select bb.* from
    (select aa.*, row_number()  over(partition by aa.orderYear order by aa.totalamount desc) as num
    from 
    (
    select to_char(order_date,'yyyy') orderYear ,customer_id,sum(order_total) totalamount
    from orders a
    group by to_char(order_date,'yyyy'),customer_id
    order by totalamount desc
    ) aa) bb
    where bb.num<=5