比如我一个表有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%
。。
-------------
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%
。。
解决方案 »
- 动态查询多列,然后求每行平均值问题
- 远程登陆oracle
- 执行oracle存储过程的问题
- 这句插入语句错在哪里? INSERT INTO TB_ACTIVE (uid, environ, time) VALUES (201378615, 0, Sun Jul 16 23:59:58 CST 2006)
- 请指教!关于Oracle左连接写法的问题?
- 如何向现有的数据库中中导入数据表?
- 请告诉我, 我已有一个用来针对sqlserver数据库创建的sql脚本文件,如何快速地得到针对oracle的sql脚本文件??
- 提示错误 tns:protocal adapter error pb65连oracle817时,用plsql是可以联的
- 有没有人用pl/sql developer
- oracle8i的存储过程问题
- 如何更改表空间?
- 控制文件丢失
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
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 显示%。要怎么写啊,我老出语法错误。。
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 显示%。要怎么写啊,我老出语法错误。。
,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 这样可以吗?
之所以要用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)
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感谢你解决了我的问题,我又学了一招,呵呵