SQL 查询重复类别(计算每个类别中的项目)并且必须包含至少一种特定类型

SQL Query for Repeated category(count items in each category) and has to contain atleast one specific type

SQL 对每个 category_id 执行重复 item_id 的查询。为此,查询将是

Select item_id, category_id, COUNT(*) 
from table 
group by category_id, item_id 
having count(*)>1 

但是我如何更改代码以仅过滤重复计数中至少具有一种类型 A 的代码。

您也可以计算 HAVING 子句中的 "A":

Select item_id, category_id, COUNT(*) 
from table 
group by category_id, item_id 
having count(*) > 1 and sum(case when type = 'A' then 1 else 0 end) > 0;

根据您的示例数据,您可以将其缩短为:

Select item_id, category_id, COUNT(*) 
from table 
group by category_id, item_id 
having count(*) > 1 and min(type)= 'A' ;

但这取决于您使用的实际名称。