请考虑这种情况:
我有三个这样的表:我的主表包含详细信息数据:
ID CityCode Switch1 Switch2 Price Desc
--------------------------------------------------------------------------
1 202 10 1 2342 Some Desc
2 202 10 1 12 Some Desc
3 202 12 1 22 Some Desc
4 203 10 1 46 Some Desc
5 203 12 1 23 Some Desc
6 203 12 1 12 Some Desc
7 205 14 1 6758 Some Desc
ID
是Identity
库伦并且是Primary Key
。
另一个表是CityTypes
:
CityCode CityName CityType
--------------------------------------------------
202 City 1 1
203 City 2 2
204 City 3 1
205 City 4 1
第三个表是TotalCount
:该表显示应为特定城市中的特定商品插入的总计数记录。
Switch1 Switch2 Name CityType TotalCount
-------------------------------------------------------------------
10 1 Good 1 1 10
10 1 Good 1 2 5
10 1 Good 1 3 3
11 1 Good 2 1 12
11 1 Good 2 2 8
11 1 Good 2 3 5
12 1 Good 3 1 10
12 1 Good 3 2 5
12 1 Good 3 3 3
13 1 Good 4 1 10
13 1 Good 4 2 5
13 1 Good 4 3 3
14 1 Good 5 1 10
14 1 Good 5 2 5
14 1 Good 5 3 3
Switch1
+ Switch2
+CityType
是主键
我想编写一个返回此结果的查询:
CityName GoodName InsertedCount TotalCount InsertedCount-TotalCount
------------------------------------------------------------------------------------
City 1 Good 1 2 10 -8
我不知道如何为这个查询关联这些表。我写了这个查询:
SELECT CityCode,Switch2,Switch1,COUNT(ID)
FROM tblMain
GROUP BY CityCode,Switch2,Switch1
ORDER BY CityCode,Switch2,Switch1
但我不知道如何将其与其他表格联系起来