Sql 获取记录计数并从结果集中的其他表中检索一些值 请考虑这种情况:

Sql 获取记录计数并从结果集中的其他表中检索一些值 请考虑这种情况:,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我有三个这样的表:包含详细数据的主表: ID CityCode Switch1 Switch2 Price Desc -------------------------------------------------------------------------- 1 202 10 1 2342 Some Desc 2

我有三个这样的表:包含详细数据的主表:

 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
coulmn,是
主键

另一个表是
citypes

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
但我不知道如何将此表与其他表关联起来

SELECT 
    ct.Cityname,total.name,COUNT(total.*) as insertedcount,sum(total.totalcount) as totalcount, 
    COUNT(total.*)-sum(total.totalcount) as diff
FROM   
    tblMain as main inner join citytypes as ct on main.citycode=ct.citycode
    inner join totalcount as total on ct.citytype=total.citytype
    and main.switch1=total.switch1 and main.switch2=total.switch2 
GROUP BY  ct.Cityname,total.name
ORDER BY ct.Cityname,total.name
看看这个

选择ct.cityname、tc.GoodName、COUNT()作为InsertedCount,选择sum(tc.totalcount)作为totalcount, COUNT()-总和(tc.totalcount)为'InsertedCount totalcount' 来自tblmain tm 左连接城市类型ct on ct.citycode=tm.citycode 左连接tc.citytype=ct.citytype和tm.switch1=tc.switch1和tm.switch2=tc.switch2上的totalcount tc
按ct.cityname、tc.GoodName分组

我认为
SUM
不应用于此查询。
SUM(total.totalcount)
将对
totalcount
表中的每一行进行求和。特定的
开关1
开关2
城市代码的
TotalCount
表中的值应直接放入结果集中,无需聚合。或者我编辑我的问题。在第一个表中,
ID
是主键,我认为
SUM
不应用于此查询。
SUM(total.totalcount)
将对
totalcount
表中的每一行进行求和。特定
Switch1
Switch2
CityCode
TotalCount
表中的值应直接放入结果集中,无需聚合