急求解决SQL问题。
比如
select name1,value1,value2 from table1 group by name1
查询出来结果如下:
name1 value1 value2
XXXX 2 3
YYYY 2 10
我希望通过一条SQL语句查询如下结果:
name1 value1 value2 BL (单位%)
XXXX 2 3 50
YYYY 2 10 400
"BL" 这一列 的值是 ( (value2-value1)/ value1 ) *100 的出来
谢谢了。
------解决方案--------------------
select name1,sum(xxxx_1) as value1,sum(xxxx_2) as value2,((sum(xxxx_2)-sum(xxxx_1))/ sum(xxxx_1)) *100
from table1
group by name1
xxxx_1,xxxx_2自己替换
------解决方案--------------------select name1,value1,value2,case value1 when 0 then 0 else cast( (value2-value1)/ cast(value1 as decimal(10,2))*100 as int) end from table1 group by name1
------解决方案--------------------select name1,value1,value2,
case when value1<>0 then (value2-value1)*100/ value1 end as BL
from table1