tb表格式如下
公司编号   账单号       金额   产品编号
  1        BILL001      10       P1
  1        BILL001      20       P1
  1        BILL001      20       P2
  1        BILL002      35       P1
  2        BILL001      10       P1按照公司编号 和 账单号 分组 结果应该如下
公司编号   账单号      产品1     产品2
 1         BILL001      30        20
 2         BILL002      35
 2         BILL001      10但是我写了以下SQL 加了1个sum速度就很慢 或者数据出来都不对select a.公司编号,a.账单号,sum(b1.金额) as 产品P1,sum(b2.金额) as 产品P2
from tb a
left join tb b1 on a.公司编号=b1.公司编号 and a.账单号=b.账单号
left join tb b2 on a.公司编号=b2.公司编号 and a.账单号=b.账单号
where b1.产品编号='P1' and b2.产品编号='P2'
group by  a.公司编号,a.账单号格式用left join 关联  

解决方案 »

  1.   

    本帖最后由 libin_ftsafe 于 2009-12-20 22:48:31 编辑
      

  2.   

    实际情况要比这个复杂 我想还是要用到LEFT JOIN 和 where 条件的 因为产品编号是取一段的期待LEFT JOIN版本```
      

  3.   

    /*====================================================*/
    -- Author: Ken Wong
    -- Create date: 2009-12-20 22:47:42
    -- Description:
    /*====================================================*/
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([公司编号] int,[账单号] varchar(7),[金额] int,[产品编号] varchar(2))
    insert [tb]
    select 1,'BILL001',10,'P1' union all
    select 1,'BILL001',20,'P1' union all
    select 1,'BILL001',20,'P2' union all
    select 1,'BILL002',35,'P1' union all
    select 2,'BILL001',10,'P1'
    /*
    公司编号  账单号      产品1    产品2 
    1        BILL001      30        20 
    2        BILL002      35 
    2        BILL001      10 
    */
    select 公司编号, 账单号,
    sum(case [产品编号] when 'P1' then [金额] else 0 end) as 产品1,
    sum(case [产品编号] when 'P2' then [金额] else 0 end) as 产品2
    from [tb]
    group by 公司编号, 账单号
    order by 公司编号
    -------------------------
    1 BILL001 30 20
    1 BILL002 35 0
    2 BILL001 10 0
      

  4.   

    能不能用LEFT JOIN的格式写出来呢~~
      

  5.   

    select 
        公司编号,
        账单号,
        sum(case 产品编号 when 'P1' then 金额 end) as 产品1,
        sum(case 产品编号 when 'P2' then 金额 end) as 产品2 
    from 
        tb 
    where
        ......
    group by 
        公司编号,账单号
      

  6.   

    条件加后面就行了 为什么要left join?
      

  7.   

    本帖最后由 libin_ftsafe 于 2009-12-20 22:59:38 编辑
      

  8.   

    因为可能P1 P2 P3 P4 P5是A类产品
            P6 P7 P8 P9 P10是B类产品所以我要用sum(b1.金额)然后where条件 b1.产品编号 in (P1,P2,P3,P4,P5)所以要关联表 case 貌似不行
      

  9.   

    select 
        isnull(a.公司编号,b.公司编号) as 公司编号,
        isnull(a.账单号  ,b.账单号  ) as 账单号  ,
        a.产品P1,
        b.产品P2 
    from 
        (select 公司编号,账单号,sum(金额) as 产品P1 from tb where 产品编号='P1' group by 公司编号,账单号) a
    full outer join
        (select 公司编号,账单号,sum(金额) as 产品P2 from tb where 产品编号='P2' group by 公司编号,账单号) b
    on 
        a.公司编号=b.公司编号 and a.账单号=b.账单号 
    order by
        公司编号,账单号 
      

  10.   

    select 公司编号, 账单号,
    sum(case [产品编号] when 'P1' then [金额] end) as 产品1,
    sum(case [产品编号] when 'P2' then [金额] end) as 产品2
    from [tb]
    group by 公司编号, 账单号
      

  11.   

    谢谢libin_ftsafe
    但是有2个SUM字段时速度很慢 数据不对这样是正确的
    select a.公司编号,a.账单号,sum(b1.金额) as A类产品
    from (select distinct 公司编号,账单号 from tb) a 
    left join tb b1 on a.公司编号=b1.公司编号 and a.账单号=b.账单号 
    where b1.产品编号 in (P1,P2) group by  a.公司编号,a.账单号 2个SUM 就是错误的
    select a.公司编号,a.账单号,sum(b1.金额) as A类产品,sum(b2.金额) as B类产品
    from (select distinct 公司编号,账单号 from tb) a 
    left join tb b1 on a.公司编号=b1.公司编号 and a.账单号=b.账单号 
    left join tb b2 on a.公司编号=b2.公司编号 and a.账单号=b.账单号 
    where b1.产品编号 in (P1,P2)  and b2.产品编号 in (P3,P4)
    group by  a.公司编号,a.账单号 
      

  12.   

    select 
        isnull(a.公司编号,b.公司编号) as 公司编号,
        isnull(a.账单号  ,b.账单号  ) as 账单号  ,
        a.产品P1,
        b.产品P2 
    from 
        (select 公司编号,账单号,sum(金额) as 产品P1 from tb where 产品编号='P1' group by 公司编号,账单号) a
    full outer join
        (select 公司编号,账单号,sum(金额) as 产品P2 from tb where 产品编号='P2' group by 公司编号,账单号) b
    on 
        a.公司编号=b.公司编号 and a.账单号=b.账单号 
    order by
        公司编号,账单号 
      

  13.   

    create table #tb(公司编号 char(2),账单号 char(7),金额 int,产品编号 char(2))insert #tb select '1','bill001',10,'p1' union all
    select '1','bill001',20,'p1' union all
    select '1','bill001',20,'p2' union all
    select '1','bill002',35,'p1' union all
    select '2','bill001',10,'p1'
    select 公司编号,账单号,sum(case when 产品编号='p1' then 金额 else 0 end) as 产品1,sum(case when 产品编号='p2' then 金额 else 0 end) as 产品2
    from #tb
    group by 公司编号,账单号
    order by 公司编号