0

次のようにレコードを取得しています: provider_id location_id | service_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 のクエリを作成し、それに従ってレコードを取得しました。

しかし、私は場所ごとに 3 つの service_ids 387、397、および 700 がすべて存在する必要があります。現在、1 つまたは 2 つの service_id を持つ場所を取得しています。これらのレコードを除外したい。そのため、検索が行った 3 つのサービスすべてをサポートするすべての場所を取得します。

また、レコードには、サービスの (同じまたは異なる) 場所で作業している複数のプロバイダーがいます。

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 に答える