0
すべてのあなたのための簡単な質問SQLの教祖:私は以下のテーブル構造(無関係の列を含まない)を持っています。合計のためのSQLクエリ
Area -- AreaID, AreaName
District -- DistrictID, DistrictName, AreaID
Office -- OfficeID, OfficeName, DistrictID
Customer -- CustomerID, OfficeID
入力パラメータとしてAreaIDを指定すると、Areaでグループ化され、次にDistricによってグループ化された顧客数を取得できる必要があります。
DistrictID1 DistrictName1 Count_of_customers
DistrictID2 DistrictName1 Count_of_customers
...
と、このようなクエリは、エリア内の顧客のカウントのためにトリック
Select D.DistrictID, D.DistrictName, Count(*)
FROM District AS D
INNER JOIN Office AS O
ON D.DistrictID = O.DistrictID
INNER JOIN Customers AS C
ON O.OfficeID = C.OfficeID
WHERE D.AreaID = 1234
GROUP BY D.DistrictID, D.DistrictName
を行う必要があります
Area1 Count_of_customers
Area2 Count_of_customers
....