当订单项匹配(在MySql中)时,我想对下表的价格求和。 表1-
Customers. OrderItem. Price
Customer A. Pizza. 300
Customer B. Pizza. 500
Customer A. Pizza. 200
Customer B. Pizza. 300
Customer A. Softdrink. 50
Customer A. Softdrink. 20
Customer B. Softdrink. 30
Customer B. Softdrink. 60
要获得像这样的新表:-
Customer. Pizza. Softdrink
Customer A. 500. 70
Customer B. 800. 90
在MySql中。
答案 0 :(得分:0)
您可以使用条件聚合:
SELECT
Customer,
SUM(CASE WHEN OrderItem = 'Pizza' THEN Price ELSE 0 END) AS Pizza,
SUM(CASE WHEN OrderItem = 'Softdrink' THEN Price ELSE 0 END) AS Softdrink
FROM yourTable
GROUP BY
Customer;