有一张表CHARGES,记录如下:
CODE AR_AP COMPANY AMOUNT
ABC AR CUSTOMER 100.00
ABC AP VENDOR1 80.00
XYZ AR CUSTOMER 50.00
XYZ AP VENDOR2 30.00
现想用SQL实现如下结果,请问如果实现。谢谢!
ABC CUSTOMER 100.00 VENDOR1 80.00
XYZ CUSTOMER 50.00 VENDOR2 30.00
------解决方案--------------------
select t1.code, t1.COMPANY, t1.AMOUNT ,t2.COMPANY, t2.AMOUNT from
CHARGES t1, CHARGES t2 where t1.CODE = t2.CODE
如果同一个code 有多条数据,那么建议用存储过程解决
------解决方案--------------------
看起来只有AP,AR两个值,那么
SELECT CODE, MAX(CASE WHEN AR_AP = 'AR' THEN COMPANY END), MAX(CASE WHEN AR_AP = 'AR' THEN AMOUNT END),
MAX(CASE WHEN AR_AP = 'AP' THEN COMPANY END), MAX(CASE WHEN AR_AP = 'AP' THEN AMOUNT END)
GROUP BY CODE;