1

SQLクエリの実行方法について質問があります。ここで使用しているサンプルデータベースを作成しました。支援を希望するすべての人のために、物事をシンプルに保つように努めています。

Officer              Permit               Vehicle              Dispatch

Oid | Dname | Rank   Oid | Type | Model   Vid | Type | Model   Did | Oid | Location
------------------   ------------------   ------------------   --------------
1   | John  |  Jr    1     D1     Ford    1     D1     Ford    1     1      Hill
2   | Jack  |  Sr    1     D2     Ford    2     D2     Ford    2     2      Beach
3   | Jay   |  Jr    2     D1     Ford    3     D3     Ford    3     3      Post
4   | Jim   |  Jr    3     D1     Ford    4     D4     Ford    4     1      Beach
5   | Jules |  Sr    5     D1     Ford    5     D5     Ford    5     2      Hill
                     1     D3     Ford                         6     4      Post
                     2     D2     Ford                         7     5      Hill
                     4     D1     Ford                         8     5      Beach
                     1     D5     Ford                         9     2      Post

テーブル間の関係は次のとおりです。

Officer - lists the officer by OID(officer ID)/Name/Rank where Sr is highest, Jr is lowest.
Permit - Officers are required to have a permit depending on the vehicle they will be using, Oid for Officer ID, Type for the vehicle and Model.
Vehicle - Vid for vehicle ID, Type and Model
Dispatch - Did for Dispatch ID, keeps track of which officer (Oid) was dispatched to which location (Location)

質問:ここからいくつかのことを知る必要があります。まず、どの役員がすべての車種を運転することを許可されているかを知るにはどうすればよいですか?第二に、どの役員が派遣されたすべての場所に派遣されたかを知るにはどうすればよいですか?

これらの2つのクエリを作成することは、私にとって悪夢でした。さまざまなテーブルを結合しようとしましたが、どちらからも最も頻繁に発生する要素を取得できません(方法がわかりません)。

4

1 に答える 1

2

最初の質問:

select Oid, count(*) type_count
from Permit
group by Oid
having type_count = (select count(distinct Type, Model) from Vehicle)

2番:

select Oid, count(*) location_count
from Dispatch
group by Oid
having location_count = (select count(distinct Location) from Dispatch)

パターンを見ますか?

于 2012-10-09T19:45:52.160 に答える