我有一个查询来构建MySQL数据透视表,它没有给我我想要的东西。我要构建的是这个,而MySQL中的数据如下所示
当我运行查询时,它会产生这不是我想要的东西
这是查询
select
o.name,
case when p.name= "Cash" then s.amount end as Cash,
case when p.name= "M-PESA" then s.amount end as mpesa,
s.customer_credit
FROM outlet o
INNER JOIN sale s ON s.outlet_id = o.id
INNER JOIN payment_type p ON s.payment_id = p.id`
你很亲密 您只需要打开聚合:
select
o.name,
sum(case when p.name= 'cash' then s.amount else 0 end) as cash,
sum(case when p.name= 'm-pesa' then s.amount else 0 end) as mpesa,
sum(s.customer_credit)
from outlet o
inner join sale s on s.outlet_id = o.id
inner join payment_type p on s.payment_id = p.id
group by o.name
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句