使用FULL JOIN

有一个Table的内容如下

项目 金额 公式
Item1 20.00 -1
Item2 100.00 1
Item3 50.00 -1

需要将它显示为

项目 付款金额 项目 罚款金额
Item2 100.00 Item1 20.00
Item3 50

其中公式=-1的是罚款,请问这个SQL如何写?

---------------------------------------------------------------

比较麻烦!!

会不会出现:

项目 金额 公式
Item1 20.00 -1
Item2 100.00 1
Item2 30.00 1
Item3 50.00 -1

即项目是不是可能重复,或者告诉我们你的表的主健.

------------------------------------------------

如果项目不重复:

select x.项目 as 付款项目,x.金额 as 付款金额,y.项目 as 罚款项目,y.金额 as 罚款金额
from
(
select (select count() from tablename where 项目<=a.项目 and 公式<>-1) as id,项目,金额 from tablename a where 公式<>-1
) as x full join
(
select (select count(
) from tablename where 项目<=b.项目 and 公式=-1) as id,项目,金额 from tablename b where 公式=-1
) as y
on x.id=y.id

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus