[英]mysql group by using two columns
My query: 我的查询:
SELECT
tm.ManufacturerName,sum(tb.Quantity) AS qty,
tb.AfterDiscount AS AfterDiscount
FROM tblbasket AS tb
INNER JOIN tblstock AS ts
ON ts.ProductCode = tb.ProductCode
AND tb.Status=3
LEFT JOIN tblmanufacturer AS tm
ON tm.ManufacturerID=ts.ManufacturerID
GROUP BY ts.ManufacturerID,AfterDiscount
ORDER BY qty desc
Outputs this: 输出此:
ManufacturerName qty AfterDiscount
MOSER BAER HOME VIDEO 48 57.6
MOSER BAER HOME VIDEO 39 28.8
MOSER BAER HOME VIDEO 28 115.2
MOSER BAER HOME VIDEO 27 259.2
MOSER BAER HOME VIDEO 26 374.4
SAREGAMA INDIA LIMITED 25 135
SYMPHONY HOME VIDEO 22 36
MOSER BAER HOME VIDEO 20 144
SAREGAMA INDIA LIMITED 19 1282.5
SAREGAMA INDIA LIMITED 18 67.5
MOSER BAER HOME VIDEO 18 172.8
MOSER BAER HOME VIDEO 16 460.8
SYMPHONY HOME VIDEO 16 45
RAJ VIDEO VISION 16 71.1
MOSER BAER HOME VIDEO 15 86.4
SAREGAMA INDIA LIMITED 15 202.5
MASTER ACCESSORIES 13 90
MOSER BAER HOME VIDEO 13 89.1
MELODY RECORDING 13 31.5
MOSER BAER HOME VIDEO 13 27
What I want is this: 我想要的是:
ManufacturerName qty AfterDiscount
MOSER BAER HOME VIDEO 190 1568
SAREGAMA INDIA LIMITED 77 1686
SYMPHONY HOME VIDEO 38 81
RAJ VIDEO VISION 16 71.1
MASTER ACCESSORIES 13 90
MELODY RECORDING 13 31.5
My query does not display the correct results. 我的查询没有显示正确的结果。 What should I change in my query?
我应该更改查询什么?
I guess I should sum quantity and price, and group by quantity and price, and order by quantity. 我想我应该对数量和价格求和,并按数量和价格分组,并按数量排序。
Any ideas or suggestion? 有什么想法或建议吗?
Remove the AfterDiscount group by. 删除AfterDiscount组。 This gives you a unique row for all ManufacturerId,AfterDiscount combinations which is what your output reflects.
这为所有ManufacturerId,AfterDiscount组合提供了唯一的一行,这是您的输出所反映的。
You just have to GROUP BY ManufacturerID and SELECT SUM(AfterDiscount) to roll up that column and any other column that needs to be aggregated. 您只需要GROUP BY ManufacturerID和SELECT SUM(AfterDiscount)即可汇总该列和任何其他需要聚合的列。
SELECT tm.ManufacturerName,sum(tb.Quantity) as qty,SUM(tb.AfterDiscount) as AfterDiscount from tblbasket as tb inner join tblstock as ts on ts.ProductCode = tb.ProductCode and tb.Status=3 left join tblmanufacturer as tm on tm.ManufacturerID=ts.ManufacturerID
group by ts.ManufacturerID
order by qty desc
如果查询中的某些字段没有“分组”,则您将无法获得所需的分组,特别是必须按制造商名称分组,并且它将仅对出现的每个唯一性进行分组...然后添加总和/每个字段都需要组购买。每个字段上必须有组或总和
You could actually take a slightly different approach here which would group by both columns concatenated. 您实际上可以在这里采取稍微不同的方法,该方法将两个列串联在一起。 I ran into this where I was trying to use a comparison operator on a calculated value in a left join and it wouldn't work, so came up with concatenating two columns and grouping on the resulting value.
我遇到了这种情况,我试图在左联接中对计算值使用比较运算符,但它不起作用,因此想出了将两列连接起来并根据结果值进行分组的方法。
SELECT
tm.ManufacturerName,sum(tb.Quantity) AS qty,
tb.AfterDiscount AS AfterDiscount,
concat_ws(':', tm.ManufacturerID, tb.AfterDiscount) as ConcatManufAfterDisc
FROM tblbasket AS tb
INNER JOIN tblstock AS ts
ON ts.ProductCode = tb.ProductCode
AND tb.Status=3
LEFT JOIN tblmanufacturer AS tm
ON tm.ManufacturerID=ts.ManufacturerID
GROUP BY ConcatManufAfterDisc
ORDER BY qty desc
It might seem like a non-ideal solution but the grouping should work and it's a useful workaround. 看来这是一个不理想的解决方案,但分组应该有效,这是一个有用的解决方法。
change this 改变这个
group by ts.ManufacturerID,AfterDiscount
to 至
group by ts.ManufacturerID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.