有表p,人员信息表,有4个字段,a人员姓名,b人员年龄,c人员工资 ,d所属省份代码,其中d字段有少数人员没有填写
有表s,省份表,有两个字段,e省份代码,f省份名称1,查询年龄最小的10个人员的姓名、年龄、工资及省份名称的sqlselect a,b,c,f from p,s where p.d=s.e 
order by b;
应该是按照年龄从小排到大,找出前十个,怎么能找出前十个?2,查询出平均工资最低的3个省份的人员的平均年龄的sql
3,以10岁为一档,也是<10岁是一档,>=10岁且<20岁为一档,依次类推,查询出各档的平均年龄及平均工资的sql.请教。谢谢。

解决方案 »

  1.   

    select a,b,c,f from p,s where p.d=s.e 
    order by b;
    应该是按照年龄从小排到大,找出前十个,怎么能找出前十个?--------------
    select a,b,c,f from p,s where p.d=s.e  order by b where rownum<11 
      

  2.   

    select avg(b) from p where d in (select d from p order by avg(c) where rownum<4)
      

  3.   


    ?1
    select t.a,t.b,t.c,t.f
    from (
         select a,b,c,f,rownum rn
         from p,s 
         where p.d=s.e 
         order by b ) t
    where rn<=10;
      

  4.   

    select a,b,c,f from p,s where p.d=s.e and rownum<11 order by b
      

  5.   

    select * from (select a,b,c,f from p,s where p.d=s.e 
    order by b) where rownum<11 ;--------这个是最准确的了。。呵呵
      

  6.   


    --你应该给写测试代码:
    --2.查询出平均工资最低的3个省份的人员的平均年龄的sql
    select avg(d)
    from p
    where d in(
         select d,avg(c) avg_c
         from p
         order by avg_c
         group by d
         where rownum<=3)
      

  7.   


    1.
    select * from (select a, b, c, f,rownum rn from p, s where p.d = s.e(+) order by b) where rn<=10;2.
    select * from (select d,avg(c) s,avg(b) age,rownum rn from p group by d order by avg(c)) t where rn<=33.
    select  avg(d) 平均年龄,avg(c) 平均成绩 from p group by floor(b/10)
      

  8.   

    select avg(p.b),avg(p.c),
    (case when p.b < 10 then 1dang
          when p.b >= 10 and p.b <20 then 2dang
          when p.b >= 20 and p.b <30 then 3dang
          when p.b >= 30 and p.b <40 then 4dang
          when p.b >= 40 and p.b <50 then 5dang
         end) dangci
    from p 
    group by (case when p.b < 10 then 1dang
          when p.b >= 10 and p.b <20 then 2dang
          when p.b >= 20 and p.b <30 then 3dang
          when p.b >= 30 and p.b <40 then 4dang
          when p.b >= 40 and p.b <50 then 5dang
         end)
      

  9.   

    select p.a,p.b.p.c,s.f 
    from p 
    left join s 
    on(p.d=s.e) 
    where ROWNUM <=10 order by p.b asc
    这是在oracle中的写法
      

  10.   


    --查询出年龄最小的4个人员的姓名、年龄、工资及省份名称的sql
    select * from (select a,b,c,f from p,s where p.d=s.e order by b) where rownum <5;--查询出平均工资最低的2个省份的人员的平均年龄的sql
    select * from (select d,avg(c) 平均工资,avg(b) 平均年龄 from p group by d order by 平均工资) where rownum<=3--以10岁为一档,也就是<10岁是一档,>=10岁且<20岁为一档,
    --依次类推,查询出各档的平均年龄及平均工资的sql
    select  avg(b) 平均年龄,avg(c) 平均成绩 from p group by floor(b/10);