比如我一个表有2列,结构是这样的:a      b   
-------------
10     50
20     35
15     30  
写一个select语句,查询的结果如下a        b         c(b-a)          d(b+a)          e(a*b)           f(a/b)按%显示
----------------------------------------------------------------------------------
10       50          40             60              500                20%
。。  

解决方案 »

  1.   

    这个太简单了
    select a,b,b-a c,b+a d,a*b e ,to_char(decode(b,0,0,round(100*a/b,4)))||'%' f
    from table
      

  2.   


    CREATE OR REPLACE FORCE VIEW view_table(单位, 日电量, 月电量,年电量)
    AS 
        select sname,
               sum(decode(trucn(sdate),trunc(sysdate),sdl,0)) day_dl, 
               sum(decode(to_char(sdate,'yyyymm'),to_char(sysdate,'yyyymm'),sdl,0)) month_dl, 
               sum(decode(to_char(sdate,'yyyy'),to_char(sysdate,'yyyy'),sdl,0)) year_dl 
           from a 
           group by sname就你早上给我的这个例子,我要多一个列出来,随便叫个名字,用month_dl/year_dl 显示%。要怎么写啊,我老出语法错误。。
      

  3.   

    CREATE OR REPLACE FORCE VIEW view_table(单位, 日电量, 月电量,年电量)
    AS 
        select sname,
               sum(decode(trucn(sdate),trunc(sysdate),sdl,0)) day_dl, 
               sum(decode(to_char(sdate,'yyyymm'),to_char(sysdate,'yyyymm'),sdl,0)) month_dl, 
               sum(decode(to_char(sdate,'yyyy'),to_char(sysdate,'yyyy'),sdl,0)) year_dl 
           from a 
           group by sname就你早上给我的这个例子,我要多一个列出来,随便叫个名字,用month_dl/year_dl 显示%。要怎么写啊,我老出语法错误。。 
      

  4.   

    to_char(decode(sum(decode(to_char(sdate,'yyyy'),to_char(sysdate,'yyyy'),sdl,0))
    ,0,0,
    round(100*sum(decode(to_char(sdate,'yyyymm'),to_char(sysdate,'yyyymm'),sdl,0)) /
    sum(decode(to_char(sdate,'yyyy'),to_char(sysdate,'yyyy'),sdl,0)),4))) ¦ ¦'%' f 这样可以吗?
      

  5.   

    其实你可以在外面再包层sql,这样看得比较清楚
    之所以要用decode,主要是防止除数为0,不过照你的数据,year_dl应该不会出现为0的情况,所以也可以直接用
    to_char(100*round(month_dl/year_dl,4))||'%' rateselect aa.*,to_char(decode(aa.year_dl,0,0,100*round(month_dl/year_dl,4)))||'%' rate
    from( 
    select sname,
               sum(decode(trucn(sdate),trunc(sysdate),sdl,0)) day_dl, 
               sum(decode(to_char(sdate,'yyyymm'),to_char(sysdate,'yyyymm'),sdl,0)) month_dl, 
               sum(decode(to_char(sdate,'yyyy'),to_char(sysdate,'yyyy'),sdl,0)) year_dl 
           from a 
           group by sname)
      

  6.   

    我做出来了
    create or replace view v_dltj as
    select h.name 名称,
      round(sum(decode(to_char(h.hddate,'yyyymmdd'),to_char(sysdate-2,'yyyymmdd'),h.dliang,0)),2) 日电量, 
      round(sum(decode(to_char(h.hddate,'yyyymm'),to_char(sysdate-2,'yyyymm'),h.dliang,0)),2) 月累计, 
      round(sum(decode(to_char(h.hddate,'yyyy'),to_char(sysdate-2,'yyyy'),h.dliang,0)),2) 年累计,
      round(j.total*10000, 2) 年计划, 
      to_char(round(decode(j.total,0,0,
                     round(100*sum(decode(to_char(h.hddate,'yyyy'),to_char(sysdate-2,'yyyy'),h.dliang,0))/(j.total*10000),4)), 2)) | |'%' 年完成率 from hdbalanceddjh h, jh_data_m j
    where h.name in ('三峡','葛洲坝','隔河岩','丹江','阳逻三期','水布垭','襄樊','荆门','姚孟','姚孟二厂','沁北','邙山','三门峡','多宝山','丰鹤','周湾','五强溪','三板溪','湘潭','金竹山','丰城','黄金埠','二滩')
    and h.name = j.mc
    group by h.name, j.total感谢你解决了我的问题,我又学了一招,呵呵