0

我得到如下记录:provider_id location_id | 服务标识

21 | 42 | 387

32 | 42 | 387

23 | 42 | 397

45 | 42 | 397

25 | 42 | 700

25 | 53 | 397

27 | 53 | 700

我想进一步过滤掉它,如下所示。我查询了 service_id 387,397 和 700 并据此获取记录。

但我希望每个位置的所有三个 service_id 387、397 和 700 都必须存在。现在我正在获取具有 1 或 2 个 service_id 的位置。我想过滤掉这些记录。这样我将获得支持搜索所针对的所有三种服务的所有位置。

还记录有多个提供者在(相同或不同的)服务位置工作。

4

1 回答 1

1

The following query does what I think you want:

select location, count(distinct serviceid) as numserviceids
from t
where serviceid in (387, 397, 700)
having count(distinct serviceid) = 3

It selects the service ids you are looking for. It then groups them by location, counting the number of service ids at each location, and returning only the ones that have all three.

于 2012-06-05T13:44:36.507 回答