我希望查询此事务表中的所有事务数据并相应地对它们求和。表格中的数据如下:
Company PO Document No Document Type Amount
001 PO-001 222 OR 2.2
001 PO-001 222 OR 3.3
001 PO-001 444 OP 4.4
001 PO-001 444 OP 5.5
001 PO-002 555 OP 1.2
001 PO-002 666 OR 1.2
只要相同采购订单编号的总金额但不同的文件类型没有理货我应该将它们显示为以下输出:
Company PO Document_No DocumentType_OR Amount DocumentType_OP Amount_PO
001 PO-001 222 OR 5.5 OP 9.9
你有没有建议我该怎么做?非常感谢你。
答案 0 :(得分:0)
使用Group by
和Sum
Amount
select Company,PO,"Document No","Document Type",sum(Amount) as Amount
from yourtable
Where PO = 'PO-001'
group by Company,PO,"Document No","Document Type"
更新:使用conditional Aggregate
SELECT Company,
PO,
Min([Document No]),
Max(CASE WHEN "Document Type" = 'OR' THEN "Document Type" END) As Document_Type_OR,
Sum(CASE WHEN "Document Type" = 'OR' THEN amount END) AS Amount_OR,
Max(CASE WHEN "Document Type" = 'OP' THEN "Document Type" END) As Document_Type_OP,
Sum(CASE WHEN "Document Type" = 'OP' THEN amount END) AS Amount_PO
FROM Yourtable
WHERE PO = 'PO-001'
GROUP BY Company,PO