postgre交叉表查询结果使列值不匹配。这是下面使用内部联接的代码。
Postgre查询
SELECT DISTINCT
transaction_date,payee_name,v1.voucher_number,cash_advance,cash_on_hand FROM
vouchers v1
INNER JOIN(
select *
from crosstab ('select voucher_number, particular, sum(debit) from
vouchers GROUP BY voucher_number,particular ORDER BY 1,2')
as final_result("voucher_number" integer,
"cash_advance" float,
"cash_on_hand" float
)) as v2
ON v1.voucher_number = v2.voucher_number
查询结果 注意:下图中显示的突出显示的值已互换
数据库表(凭证)
注意:下表中的数据库表显示了每一行的正确值。突出显示的值是正确的,但可以使用上面显示的交叉表查询互换。
此查询返回正确的值:
select * from crosstab ('select voucher_number, particular, sum(debit) from
vouchers GROUP BY voucher_number,particular ORDER BY 1,2')
as final_result("voucher_number" integer,
"cash_advance" float,
"cash_on_hand" float
)
我上面使用内部联接的代码有什么问题?请帮忙。谢谢。