select c.sname,count(b.rid)
   from t_sinfo c,t_pellib b,t_fullib a,
   where a.tid=b.rid and a.sid=c.sid
   and b.dtime>=.....
   and b.dtime<=.....
   and b.rtype=1
   group by(c.sname)
   order by count(b.rid) desc;
这句SQL语句测试了,结果是正确的!
但是现在想去前十条数据,加上 and rownum<=10 后!结果只有7条,而且得到的sname,count(b.rid)还不对应!求解释,求解决方案!

解决方案 »

  1.   

    select * from 
    (select c.sname,count(b.rid),rownum rn
       from t_sinfo c,t_pellib b,t_fullib a,
       where a.tid=b.rid and a.sid=c.sid
       and b.dtime>=.....
       and b.dtime<=.....
       and b.rtype=1
       group by(c.sname)
       order by count(b.rid) desc)
    where 
    rn < 10
      

  2.   

    注意这一列的用意“rownum rn”,它是用来记录记录的行数,在最终选择后加了限制“where rn<10"就是从(select c.sname,count(b.rid),rownum rn
      from t_sinfo c,t_pellib b,t_fullib a,
      where a.tid=b.rid and a.sid=c.sid
      and b.dtime>=.....
      and b.dtime<=.....
      and b.rtype=1)里选出的前十行
      

  3.   

    估计楼主是直接在后面加的  你的是先给这个记录编个行号 在排序肯定不行的  
    肯定是的先排序再加行号 
    select sname,rid
    from (select sname,rid,rownum rn
    from (select c.sname,count(b.rid) rid
       from t_sinfo c,t_pellib b,t_fullib a,
       where a.tid=b.rid and a.sid=c.sid
       and b.dtime>=.....
       and b.dtime<=.....
       and b.rtype=1
       group by(c.sname)
       order by count(b.rid) desc) k) where rn<=10select sname,rid
    from (select c.sname,count(b.rid) rid
       from t_sinfo c,t_pellib b,t_fullib a,
       where a.tid=b.rid and a.sid=c.sid
       and b.dtime>=.....
       and b.dtime<=.....
       and b.rtype=1
       group by(c.sname)
       order by count(b.rid) desc)  where rownum<11