code pay payflag
00001 100.00 0
00001 50.00 1
00001 50.00 0
00002 200.00 0
pay 是 decimal,payflag 0 是付款, 1 是退款
把code相同并payflag=0的pay相加减去payflag=1的pay得到下面这下的数据
code pay
00001 100.00
00002 200.00
急着要。。在线等。。。
select code,sum(pay*(case payflag when 1 then -1 else 1 end)) as pay
from [table]
group by code
谢谢,不过我找到了更好的办法
select code,sum(if(payflag=0,pay,-1*pay))
from tt
group by code
@阿岚: if***估计是mysql自己的语法把
select code, sum(pay*(cos(PI()*payflag))) as pay from [tablename] group by code
谢谢,不过我找到了跟好的办法
select code,sum(if(payflag=0,pay,-1*pay))
from tt
group by code
select code ,
sum(pay) pay
from (
select code ,
concat( (case payflag=1 then '-' else '0') , pay) pay
from *
)
group by code
没用过mysql 根据Oracle语句 查了点资料 改的 试试行不
谢谢,不过我找到了更好的办法
select code,sum(if(payflag=0,pay,-1*pay))
from tt
group by code