select a1,(a1/(select sum(a2) from test))*100 bili from test

解决方案 »

  1.   

    select A.col_1,A.col_2,concat(to_char(100*A.col_2/B.total),'%')
    from A, (select sum(col_2) as total from A) B
      

  2.   

    declare  
    mysum number;
    begin
    select count(*) into  mysum from  A ;
    select  字段一,字段二,字段二/mysum  as  比例   from  A;
    end;
    或者
    select 字段一,字段二,字段二/(select sum(字段二) from a ) as 比例 from a
      

  3.   

    如果字段2有空值,则改为:
    select A.col_1,A.col_2,concat(to_char(100*NVL(A.col_2,0)/B.total),'%')
    from A, (select sum(col_2) as total from A) B
      

  4.   

    16:02:18 SQL> select * from t1;A          B
    - ----------
    a         10
    b         30已用时间:  00: 00: 00.16
    16:02:34 SQL> select a,b,ratio_to_report(sum(b)) over() from t1 group by a,b;A          B RATIO_TO_REPORT(SUM(B))OVER()
    - ---------- -----------------------------
    a         10                           .25
    b         30                           .75已用时间:  00: 00: 00.32
      

  5.   

    zmgowin(隐者(龙祖宗))  高人阿
      

  6.   

    这结果我能用sql语句搞定,我是指用变量怎么搞定,不过还得谢谢楼上的两位
    我这么用为何不可
    declare  a  number ; 
    begin 
      select count(*)  into  a  from  表名
      select 字段一,字段二,字段2/a  as 字段三  from  表名
    end ;
    /
    错在哪儿了啊。
      

  7.   

    多谢zmgowin(隐者(龙祖宗))给出这个函数。看来我是兜圈了。