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 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 关联
-- 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
公司编号,
账单号,
sum(case 产品编号 when 'P1' then 金额 end) as 产品1,
sum(case 产品编号 when 'P2' then 金额 end) as 产品2
from
tb
where
......
group by
公司编号,账单号
P6 P7 P8 P9 P10是B类产品所以我要用sum(b1.金额)然后where条件 b1.产品编号 in (P1,P2,P3,P4,P5)所以要关联表 case 貌似不行
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
公司编号,账单号
sum(case [产品编号] when 'P1' then [金额] end) as 产品1,
sum(case [产品编号] when 'P2' then [金额] end) as 产品2
from [tb]
group by 公司编号, 账单号
但是有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.账单号
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
公司编号,账单号
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 公司编号