我从查询中得到了这个结果集:
OrderId CustomerId ProducerId CustomerPayment ProducerPayment
1 1 3 10 5
1 1 4 10 5
1 2 3 10 5
1 2 4 10 5
我需要将此结果返回到:
OrderId UserId Payment
1 1 20
1 2 20
1 3 10
1 4 10
只需将CustomerId
和ProducerId
合并到UserId
即可。与Payment
列相同。
使用简单的select
和group by
有没有办法实现这一目标?我避免使用temp tables
,调用多个相同的queries
并进行优化。我希望这是可能的。
非常感谢
答案 0 :(得分:1)
SELECT
OrderId,
CustomerID AS UserId,
SUM (CustomerPayment) As Payment
FROM orders
UNION ALL
SELECT
OrderId,
ProducerId AS UserId,
SUM (ProducerPayment) As Payment
FROM orders
答案 1 :(得分:0)
尝试这样的事情:
select
OrderId,
CustomerId,
sum(CustomerPayment) Payment,
group_concat(OrderId separator ',') listOrders /* list all OrderID's from the user and separates these with a , */
from your_table
group by CustomerId
不知道你的查询看起来像atm?