基础比较薄弱,请求指点:一张学生数据表,有男生若干、女生若干,求男生占总人数的百分比,和女生占总人数的百分比。PS:俺在学pl/sql。恳求,高级SQL和基础SQL两种写法。谢谢!!!!

解决方案 »

  1.   

    假设你的表结构如下
    name sex
    张三   男
    李四   女
    select count(*) tot,sum(decode(sex,'男',1,0))/count(*) m_rate,
    sum(decode(sex,'女',1,0))/count(*) f_rate
     from a
      

  2.   

    我的sql server 2000 上没有函数decode().
    运行错误如下:
    服务器: 消息 195,级别 15,状态 10,行 2
    'decode' 不是可以识别的 函数名。
      

  3.   

    晕,你跑到ORACLE来问问题,结果你的数据库是MS SQL
    改成这样
    select count(*) as tot,sum(case  when sex='男' then 1 else 0 end)/count(*) as m_rate,
    sum(case  when sex='女' then 1 else 0 end)/count(*)  as f_rate
     from a
      

  4.   

    select round(100*t1.tt1/t2.tt2,2) || '%',round(100*(t2.tt2-t1.tt1)/t2.tt2,2) || '%'
           from 
                (select count(*) tt1 from test where sex='男') t1,
                (select count(*) tt2 from test) t2
      

  5.   

    谢谢hebo2005 和 BlueskyWide。
    你们都是牛人~