求一段SQL的统计代码写法?A表:(“ID”是自动编号)ID 名称 价格  商家   
1   aa   3    商家1
2   bb   5    商家2
3   cc   7    商家1
4   ee   6    商家2
B表:(“ID”是自动编号;“商品代号”是A表中的ID)ID 名称 价格 商品代号 商家  
1  cc   7      3      商家1
2  bb   5      2      商家2
3  aa   3      1      商家1
4  ee   6      4      商家2
5  aa   3      1      商家1
6  bb   5      2      商家2
7  cc   7      3      商家1
8  cc   7      3      商家1目的:从A表取出所有的商家是商家1的记录,并从B表统计出商家是商家1、且B表的“商品代号”、“名称”、“价格”分别等于A表的“ID”,“名称”,“价格”的记录次数统计,查询显示成新的数据,说着麻烦,大家可以看看例子,形成象下面的统计,如:
商家1:
ID 名称 价格 商品代号 商家  统计
1  aa    3    1       商家1  2
3  cc    7    3       商家1  3这个SQL怎么写?

解决方案 »

  1.   

    --> 测试数据:#A表
    if object_id('tempdb.dbo.#A表') is not null drop table #A表
    create table #A表([ID] int,[名称] varchar(2),[价格] int,[商家] varchar(5))
    insert #A表
    select 1,'aa',3,'商家1' union all
    select 2,'bb',5,'商家2' union all
    select 3,'cc',7,'商家1' union all
    select 4,'ee',6,'商家2'
    --> 测试数据:#B表
    if object_id('tempdb.dbo.#B表') is not null drop table #B表
    create table #B表([ID] int,[名称] varchar(2),[价格] int,[商品代号] int,[商家] varchar(5))
    insert #B表
    select 1,'cc',7,3,'商家1' union all
    select 2,'bb',5,2,'商家2' union all
    select 3,'aa',3,1,'商家1' union all
    select 4,'ee',6,4,'商家2' union all
    select 5,'aa',3,1,'商家1' union all
    select 6,'bb',5,2,'商家2' union all
    select 7,'cc',7,3,'商家1' union all
    select 8,'cc',7,3,'商家1'select * from #A表
    select * from #B表--商家1:
    --ID 名称 价格 商品代号 商家 统计
    --1 aa 3 1 商家1 2
    --3 cc 7 3 商家1 3
    select a.名称,a.价格,b.商品代号,b.商家,count(1) as 统计
    from #A表 a join #B表 b
    on a.名称=b.名称 where a.商家='商家1'
    group by a.名称,a.价格,b.商品代号,b.商家 
      

  2.   

    --> 测试数据:#A表
    if object_id('tempdb.dbo.#A表') is not null drop table #A表
    create table #A表([ID] int,[名称] varchar(2),[价格] int,[商家] varchar(5))
    insert #A表
    select 1,'aa',3,'商家1' union all
    select 2,'bb',5,'商家2' union all
    select 3,'cc',7,'商家1' union all
    select 4,'ee',6,'商家2'
    --> 测试数据:#B表
    if object_id('tempdb.dbo.#B表') is not null drop table #B表
    create table #B表([ID] int,[名称] varchar(2),[价格] int,[商品代号] int,[商家] varchar(5))
    insert #B表
    select 1,'cc',7,3,'商家1' union all
    select 2,'bb',5,2,'商家2' union all
    select 3,'aa',3,1,'商家1' union all
    select 4,'ee',6,4,'商家2' union all
    select 5,'aa',3,1,'商家1' union all
    select 6,'bb',5,2,'商家2' union all
    select 7,'cc',7,3,'商家1' union all
    select 8,'cc',7,3,'商家1'select a.id,b.名称,b.价格,a.商家,COUnT(b.名称 ) as 统计
    from #B表 b,#A表 a
    where b.商家='商家1' and a.名称=b.名称 and a.价格=b.价格
    GROUP BY b.名称,b.价格,a.id,a.商家
      

  3.   

    --> 测试数据:#A表
    if object_id('tempdb.dbo.#A表') is not null drop table #A表
    create table #A表([ID] int,[名称] varchar(2),[价格] int,[商家] varchar(5))
    insert #A表
    select 1,'aa',3,'商家1' union all
    select 2,'bb',5,'商家2' union all
    select 3,'cc',7,'商家1' union all
    select 4,'ee',6,'商家2'
    --> 测试数据:#B表
    if object_id('tempdb.dbo.#B表') is not null drop table #B表
    create table #B表([ID] int,[名称] varchar(2),[价格] int,[商品代号] int,[商家] varchar(5))
    insert #B表
    select 1,'cc',7,3,'商家1' union all
    select 2,'bb',5,2,'商家2' union all
    select 3,'aa',3,1,'商家1' union all
    select 4,'ee',6,4,'商家2' union all
    select 5,'aa',3,1,'商家1' union all
    select 6,'bb',5,2,'商家2' union all
    select 7,'cc',7,3,'商家1' union all
    select 8,'cc',7,3,'商家1'
    --先查询是否相同的,再统计
    select 名称,价格,商品代号,count(id) 数量  from #B表 b
    where exists(select 1 from #A表 a where a.id=b.商品代号  and  a.名称=b.名称 and a.价格=b.价格)
    group by 名称,价格,商品代号