简体   繁体   English

计算每个类别中的项目

[英]count items in each category

I've the Following Table Schema for table Items 我具有表项目的以下表模式

id | item_id | category_id

I've sample data in this table 我在此表中有示例数据

id  | item_id | category_id

1   | 1       | 1
2   | 1       | 2
3   | 2       | 1
4   | 2       | 2
5   | 3       | 1
6   | 1       | 1

Requirement: I need to count all those items which are repeating in each category_id. 要求:我需要计算在每个category_id中重复的所有那些项目。 In the above scenario item_id '1' is repeating in category '1'. 在上述情况下,item_id'1'在类别'1'中重复。 I need to count the invalid or repeating item_id against each category_id. 我需要针对每个category_id计算无效或重复的item_id。

I think this should do it for you: 我认为这应该为您做到:

Select item_Id, category_id
From Table
Group By item_Id, category_id
Having Count(*) > 1
SELECT category_id, item_id, COUNT(*) 
FROM table
GROUP BY category_id, item_id
HAVING COUNT(*) > 1

will give you repeating ones. 会给你重复的。 As for 'invalid' you did not define what these are. 至于“无效”,您没有定义这些是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM