SQL Server 计数分组依据
我有3张桌子。我想按 item.name 进行分组并计算它在 idetail 中找到的次数。此查询仅计算所有行,而不计算按行分组的行。
有人知道如何修复它吗?
SELECT i.name, COUNT(i.name)
FROM item AS i
INNER JOIN item_category AS ic ON i.i_category_id = ic.ic_id
INNER JOIN idetail AS id ON ic.ic_id = id.id_category_id
WHERE ic.ic_id = 1002
GROUP BY i.name
I got 3 tables. I want to group by the item.name and count the times it can be found in idetail. This query only counts all rows not the grouped by rows.
Anyone knows how to fix it?
SELECT i.name, COUNT(i.name)
FROM item AS i
INNER JOIN item_category AS ic ON i.i_category_id = ic.ic_id
INNER JOIN idetail AS id ON ic.ic_id = id.id_category_id
WHERE ic.ic_id = 1002
GROUP BY i.name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是你想要的:
This is what you want:
如果您需要所有三列并且您使用的是 SQL Server 2005+
如果您使用的是 SQL Server 2005+,则另一个更简单的版本。
If you want all three columns and you are using SQL Server 2005+
Another simpler version if you are using SQL Server 2005+.