A表
  name    csocde
  张三     12
 
   李四    23B表  name    code
  张三    12
  赵六    40求A、B汇总的数据 如 
 
  name   countcode                                             区间A (<0)   区间B(<20)  区间C(>35)
  张三     24         (A表的scocde+表的code)                    0              0           0
  李四     12           (一样的算法 只是B表没有他的数据)          0              1           0
  赵六     40               。。A表没有他的数据             0              0           1区间的算法根据  countcode来区分的   0 和1表示 不是 或者是  

解决方案 »

  1.   

    A表
      name csocde
      张三 12
      
      李四 23B表  name code
      张三 12
      赵六 40求A、B汇总的数据 如  
     
      name             countcode 
      张三                24 (A表的scocde+表的code) 0 0 0
      李四               12 (一样的算法 只是B表没有他的数据)  
      赵六               40 。。A表没有他的数据  
    如果上面的难实现 就要这个样子的也可以了! 谢谢了@
      

  2.   


    use PracticeDB
    go
    if exists (select 1 from sysobjects where name='tb_a')
    drop table tb_a
    go
    create table tb_a (name varchar(10),cscode int)
    go
    insert into tb_a
    select '张三', 12 union all
    select '李四', 23
    go
    if exists (select 1 from sysobjects where name='tb_b')
    drop table tb_b
    go
    create table tb_b (name varchar(10),code int)
    go
    insert into tb_b
    select '张三', 12 union all
    select '赵六', 40;with t
    as
    (
    select * from tb_a
    union all
    select * from tb_b
    )select name, SUM(cscode) countcode
    from t
    group by namename countcode
    李四 23
    张三 24
    赵六 40
         drop table tb_a
         drop table tb_b
      

  3.   

    上面的问题 我自己弄出来了 
    但是新的问题出来了
    A表
      name csocde     qihao       xuehoa
      张三 12           2          1477878
       
      李四 23          3           44577B表  name code
      张三 12
      赵六 40
    两张表联合起来 
      name csocde     qihao       xuehoa            code
      张三 12           2          1477878              12
       
      李四 23          3           44577                0  赵六  0           0           0                   40A表里面没有也和B表组合起来。! 表也组合A表!
      

  4.   


    use PracticeDB
    go
    if exists (select 1 from sysobjects where name='tb_a')
    drop table tb_a
    go
    create table tb_a (name varchar(10),cscode int,qidao int,xuehoa numeric(10))
    go
    insert into tb_a
    select '张三', 12 ,2,1477878 union all
    select '李四', 23,3,44577
    go
    if exists (select 1 from sysobjects where name='tb_b')
    drop table tb_b
    go
    create table tb_b (name varchar(10),code int)
    go
    insert into tb_b
    select '张三', 12 union all
    select '赵六', 40;with t
    as
    (
    select a.*,b.code
    from tb_a a left join tb_b b on a.name=b.name
    union all
    select case when a.name is null then b.name else a.name end,a.cscode,a.qidao,a.xuehoa,b.code
    from tb_a a right join tb_b b on a.name=b.name
    )
    select distinct name,ISNULL(cscode,0) cscode,ISNULL(qidao,0) qidao,ISNULL(xuehoa,0) xuehoa,ISNULL(code,0) code
    from tname cscode qidao xuehoa code
    李四 23 3 44577 0
    张三 12 2 1477878 12
    赵六 0 0 0 40
      

  5.   

    --> 生成测试数据表: [A]
    IF OBJECT_ID('[A]') IS NOT NULL
    DROP TABLE [A]
    GO
    CREATE TABLE [A] ([name] [nvarchar](10),[csocde] [int],[qihao] [int],[xuehoa] [int])
    INSERT INTO [A]
    SELECT '张三','12','2','1477878' UNION ALL
    SELECT '李四','23','3','44577'--> 生成测试数据表: [B]
    IF OBJECT_ID('[B]') IS NOT NULL
    DROP TABLE [B]
    GO
    CREATE TABLE [B] ([name] [nvarchar](10),[code] [int])
    INSERT INTO [B]
    SELECT '张三','12' UNION ALL
    SELECT '赵六','40'--SELECT * FROM [A]
    --SELECT * FROM [B]-->SQL查询如下:
    SELECT ISNULL(a.name, b.name) NAME, ISNULL(a.csocde, 0) csocde, ISNULL(qihao, 0) qihao, 
    ISNULL(xuehoa, 0) xuehoa, ISNULL(b.[code], 0) [code]
    FROM   a
    FULL JOIN b
    ON a.NAME = b.NAME 
    /*
    NAME       csocde      qihao       xuehoa      code
    ---------- ----------- ----------- ----------- -----------
    张三         12          2           1477878     12
    李四         23          3           44577       0
    赵六         0           0           0           40(3 行受影响)
    */