这是我的表格,其中存储了ID及其数量和价格。我想根据价格总结数量。如果qty为负值,则有时qty可能显示为0。因此,当按价格分组时,我不会显示数量的价格下降到0值。
id | id_item | qty | price
1 1 10 1.00
2 1 15 2.00
3 1 10 1.00
4 2 5 2.00
5 2 5 2.50
6 3 10 1.00
7 3 10 1.00
8 3 5 1.00
这是我尝试过的。
Select id_item, price, sum(qty) as total from sales group by id_item, price having total !=0;
Result
id_item | qty | price
1 20 1.00
1 15 2.00
2 5 2.00
2 5 2.50
3 10 1.00
使用concat的预期结果
id_item | qty | price
1 20,15 1.00,2.00
2 5,5 2.00.2.50
3 10 1.00
我如何获得如图所示的结果?
答案 0 :(得分:0)
我认为您需要两个聚合级别:
select id_item, group_concat(total order by price) as quantities,
group_concat(price order by price) as prices
from (Select id_item, price, sum(qty) as total
from sales
group by id_item, price
having total <> 0
) s
group by id_item;