0

请考虑这种情况:

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

 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

IDIdentity库伦并且是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

但我不知道如何将其与其他表格联系起来

4

2 回答 2

1

尝试这个

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
于 2012-07-13T09:15:19.607 回答
0

看一下这个

select ct.cityname, tc.GoodName,COUNT( ) as InsertedCount, sum(tc.totalcount) as TotalCount, COUNT( )-sum(tc.totalcount) as 'InsertedCount-TotalCount' FROM tblmain tm left join CityTypes ct on ct。 citycode = tm.citycode left join totalcount tc on tc.citytype = ct.citytype and tm.switch1=tc.switch1 and tm.switch2=tc.switch2 group by ct.cityname, tc.GoodName

于 2012-07-13T09:33:41.313 回答