产生计算列的SQL语句的写法
月份 人数 总金额
1 12030 3223
2 23900 5609
3 39000 4090
4 38009 5100
通过SQL语句产生当月增长数:人数=当前月-上月 金额=当前月-上月
每列增长率 人数列当月增长率=(当前月-上月)/当前月 金额当月增长率=(当前月-上月)/当前月
2楼: select b.rs-a.rs as 人数增长,b.je-a.je as 金额增长,
(Case when a.rs=0 then 0.0 else cast((b.rs-a.rs) as decimal)/(a.rs) end) as 人数增长率 ,
(Case when a.je=0 then 0.0 else cast((b.je-a.je) as decimal)/(a.je) end) as 金额增长率
From T4 a ,T4 b where a.MM-1=b.MM
3楼: select b.mm as 月份,b.rs-a.rs as 人数增长,b.je-a.je as 金额增长,
(b.rs-a.rs)/a.rs as 人数增长率 ,
(b.je-a.je) /a.je as 金额增长率
From T4 a ,T4 b where a.MM+1=b.MM
4楼: 有点看不懂,麻烦请教一下:T4是什么,MM是什么呢,提出问题的表中的记录是增长的,比如当前是6月份,则记录就有6条。谢谢了!!!
5楼: SELECT 人数 = 当有人数-上月人数, 金额 = 当前月金额-上月金额, 字串3
当月增长率=(当前月-上月)/当前月 金额当月增长率=(当前月-上月)/当前月
FROM 有一个表
WHERE 月份 = "一个月份"如管家婆商品管理软件免费
6楼: 同意jack391:
select b.rs-a.rs as 人数增长,b.je-a.je as 金额增长,
(Case when a.rs=0 then 0.0 else cast((b.rs-a.rs) as decimal)/(a.rs) end) as 人数增长率 ,
(Case when a.je=0 then 0.0 else cast((b.je-a.je) as decimal)/(a.je) end) as 金额增长率
From T4 a ,T4 b where a.MM-1=b.MM
其中T4是你的这个表的表名,MM是你的月份这个字段.
就说用你这个表自己和自己关联查询.
楼主试试,应该没错.
7楼: oracle 中支持case when ...语法吗 ,另外where a.mm-1=b.mm什么意思,麻烦luckysw师兄讲一下。小弟新手。多谢!!
8楼: oracle 中支持case when ...语法吗? 不知道,应该支持吧,或者是另外的一种方式支持。
where a.mm-1=b.mm什么意思?你可以看成是2张表a和b(都是你所用到的表)按照a的月份-1=b的月份来连接。
9楼: 谢谢各位师兄!!!