每 个id只出现两次,每一个id对应两个pid(a,b)。计算a/b的结果,若分母为零,则a/b为零,否则正常计算,除不尽的截取两位小数。用sql语句写
题主什么数据库?mssql用下面的
select a.id,a.pid,
case
when c.val=0 then 0
else cast(ROUND((a.val+0.1)/c.val,2) as numeric(16,2))
end as result
from test a
left join (select b.val,b.id from test b where b.pid='b')c on c.id=a.id
where a.pid='a'
有帮助麻烦点个采纳【本回答右上角】,谢谢~~
select id,((select val from 表名 where id = a.id and pid='A')/(select val from 表名 where id = a.id and pid='b')) val from 表名 a group by id