表A  group by 的数据
                统计数1          代码         名称         
                  100           001           aa           
                  67           002           bb           
                 88           003           cc            
                 90          004           dd           
    
表B  group by 的数据
                统计数2          代码         名称         
                  60           001           aa           
                  89           002           bb           
                 78          003           cc            
                 50         004           dd   想要得到的结果
代码         名称          统计数1   统计数2
001           aa           100        60
002           bb           67         89
003           cc           88         78 
004           dd           90         50 

解决方案 »

  1.   

    union all
    然后行转列!
      

  2.   

    select * from a join b on a.名称=b.名称
      

  3.   

    将A表group by后的结果集和B表group by后的结果集union起来,再使用case when 行转列
      

  4.   

    select aa.代码,aa.名称,统计数1,统计数2
    from 表a aa
    inner join
    表b bb
    on aa.代码=bb.代码
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2010-03-17 14:02:31
    -- Verstion:
    --      Microsoft SQL Server  2000 - 8.00.2055 (Intel X86) 
    -- Dec 16 2008 19:46:53 
    -- Copyright (c) 1988-2003 Microsoft Corporation
    -- Personal Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go 
    create table [a]([统计数1] int,[代码] varchar(3),[名称] varchar(2))
    insert [a]
    select 100,'001','aa' union all
    select 67,'002','bb' union all
    select 88,'003','cc' union all
    select 90,'004','dd'
    --> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    go 
    create table [B]([统计数2] int,[代码] varchar(3),[名称] varchar(2))
    insert [B]
    select 60,'001','aa' union all
    select 89,'002','bb' union all
    select 78,'003','cc' union all
    select 50,'004','dd'
    --------------开始查询--------------------------
    select a.代码,a.名称,a.统计数1,b.统计数2 from a join b on a.代码=b.代码 and a.名称=b.名称
    ----------------结果----------------------------
    /* 代码   名称   统计数1        统计数2        
    ---- ---- ----------- ----------- 
    001  aa   100         60
    002  bb   67          89
    003  cc   88          78
    004  dd   90          50(所影响的行数为 4 行)*/
      

  6.   

    declare @staticA table (code varchar(16), name varchar(64), data int)
    declare @staticB table (code varchar(16), name varchar(64), data int)
    insert into @staticA .. -- 统计脚本1
    insert into @staticB .. -- 统计脚本2select B.*, isnull(a0.data,0) as 统计数1, isnull(a1.data,0) as 统计数2
    from (select code, name from @staticA union select code, name from @staticB) as B
    left join @staticA a0 on a0.code=B.code and a0.name=B.name
    left join @staticB a1 on a1.code=B.code and a1.name=B.name
      

  7.   

    汗``我的代码也和F姐一样SELECT a.代码,a.名称,a.统计数1,b.统计数2
    FROM ta a left outer join tb b ON a.代码=b.代码
      

  8.   

    还有个小问题如果想在A表的结果里再先统计,最后再合并,怎么搞呀A表 group by  统计完了之后的数据:
    统计数量 人员的Key      状态
    262 8601800124 1000
    11 8601800124 8000
    10 8601800124 2000
    72 8601800124 1300
    675 8601800124 1200
    99 8601800130 1300
    118 8601800130 1200
    225 8601800130 1000
    3 8601800130 8000
    846 8601800175 1000
    6 8601800175 8000
    47 8601800175 1200
    1 8601800175 1300想在统计完了的数据中再统计:
    按人员的Key     按状态 1000 & 1200 求和,1300 & 2000 & 8000 求和
    得到2个值
      

  9.   

    是不是 
    select 
    人员的Key,
    总数量1=SUM(case when 状态=1000 or 状态=1200 then 统计数量 end ),
    总数量2=SUM(case when 状态=1300 or 状态=2000 or 状态=8000 then 统计数量 end )
    from 你的结果集
    group by 人员的Key
      

  10.   

    if object_id('[a]') is not null drop table [a]
    go 
    create table [a]([统计数1] int,[代码] varchar(3),[名称] varchar(2))
    insert [a]
    select 100,'001','aa' union all
    select 67,'002','bb' union all
    select 88,'003','cc' union all
    select 90,'004','dd'
    --> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    go 
    create table [B]([统计数2] int,[代码] varchar(3),[名称] varchar(2))
    insert [B]
    select 60,'001','aa' union all
    select 89,'002','bb' union all
    select 78,'003','cc' union all
    select 50,'004','dd'
    --> 测试数据:[C]
    if object_id('[C]') is not null drop table [C]
    go 
    create table [C]([统计数3] int,[代码] varchar(3),[名称] varchar(2))
    insert [C]
    select 20,'001','aa' union all
    select 30,'002','bb' union all
    select 40,'003','cc' union all
    select 45,'004','dd'select a.代码,a.名称,a.统计数1,b.统计数2 from a join b on a.代码=b.代码 and a.名称=b.名称
    如果再加个C表 怎么关联合并
      

  11.   

    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go 
    create table [a]([统计数1] int,[代码] varchar(3),[名称] varchar(2))
    insert [a]
    select 100,'001','aa' union all
    select 67,'002','bb' union all
    select 88,'003','cc' union all
    select 90,'004','dd'
    --> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    go 
    create table [B]([统计数2] int,[代码] varchar(3),[名称] varchar(2))
    insert [B]
    select 60,'001','aa' union all
    --select 89,'002','bb' union all
    select 78,'003','cc' union all
    select 50,'004','dd'
    --------------开始查询--------------------------
    select a.代码,a.名称,a.统计数1,b.统计数2 from a join b on a.代码=b.代码 and a.名称=b.名称
    001 aa 100 60
    003 cc 88 78
    004 dd 90 50
    还有个问题如果A表的结果集和B表的不一样,怎么处理呀
    少002 代码的数据
      

  12.   

    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go
    create table [a]([统计数1] int,[代码] varchar(3),[名称] varchar(2))
    insert [a]
    select 100,'001','aa' union all
    select 67,'002','bb' union all
    select 88,'003','cc' union all
    select 90,'004','dd'
    --> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    go
    create table [B]([统计数2] int,[代码] varchar(3),[名称] varchar(2))
    insert [B]
    select 60,'001','aa' union all
    --select 89,'002','bb' union all
    select 78,'003','cc' union all
    select 50,'004','dd'
    --------------开始查询 --------------------------
    select a.代码,a.名称,a.统计数1,b.统计数2 
    from a 
    left join b on a.代码=b.代码 and a.名称=b.名称
    -- 代码 名称 统计数1 统计数2
    -- 001 aa 100 60
    -- 002 bb 67
    -- 003 cc 88 78
    -- 004 dd 90 50
      

  13.   

    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go
    create table [a]([统计数1] int,[代码] varchar(3),[名称] varchar(2))
    insert [a]
    select 100,'001','aa' union all
    select 67,'002','bb' union all
    -- select 88,'003','cc' union all
    select 90,'004','dd'
    --> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    go
    create table [B]([统计数2] int,[代码] varchar(3),[名称] varchar(2))
    insert [B]
    select 60,'001','aa' union all
    --select 89,'002','bb' union all
    select 78,'003','cc' union all
    select 50,'004','dd'
    --------------开始查询 --------------------------select base.*, [统计数1], [统计数2]
    from (
      select distinct 代码, 名称 from a union select distinct 代码, 名称 from b
    ) as base
    left join a on a.代码=base.代码 and a.名称=base.名称
    left join b on b.代码=base.代码 and b.名称=base.名称
    -- 代码 名称 统计数1 统计数2
    -- 001 aa 100 60
    -- 002 bb 67
    -- 003 cc 78
    -- 004 dd 90 50