Sql 获取在多个类别中具有多个条目的用户

Sql 获取在多个类别中具有多个条目的用户,sql,sql-server,Sql,Sql Server,下面的查询不返回任何结果,我猜是因为我在同一行中对同一个值进行了两次“and”运算。如果我将其更改为“或”,那么我将得到我需要的2/3,但我需要在多个类别中有多个条目的用户-有没有一种方法可以有效地在SQL中说“给我在类别a和类别B中计数大于1的用户” 使用聚合: select top (20) t.Entity as UserId from Transactions t where t.Category in ( 'CardSwipe', 'EFT') group by t.Entity h

下面的查询不返回任何结果,我猜是因为我在同一行中对同一个值进行了两次“and”运算。如果我将其更改为“或”,那么我将得到我需要的2/3,但我需要在多个类别中有多个条目的用户-有没有一种方法可以有效地在SQL中说“给我在类别a和类别B中计数大于1的用户”

使用聚合:

select top (20) t.Entity as UserId
from Transactions t
where t.Category in ( 'CardSwipe',  'EFT')
group by t.Entity
having count(distinct t.Category) = 2;
编写查询时,不需要
Users
表,除非事务中存在非用户的“实体”,因此
join
用于过滤。

使用聚合:

select top (20) t.Entity as UserId
from Transactions t
where t.Category in ( 'CardSwipe',  'EFT')
group by t.Entity
having count(distinct t.Category) = 2;

在编写查询时,
Users
表是不必要的,除非事务中存在非用户的“实体”,因此,
join
用于过滤。

示例数据和预期结果将非常有用。示例数据和预期结果将非常有用。