create table floatData
(
date datetime not null,
cal varchar(20),
name varchar(20),
quality varchar(20),
provider varchar(20),
weight int,
amount int
)
insert into floatData
select '2012-3-5','钢材','元钢','304','A',20,600
union all select '2012-3-6','钢材','元钢','304','B',25,500
union all select '2012-3-7','钢材','元钢','316','A',15,300
union all select '2012-3-5','钢材','元钢','304','A',30,900
union all select '2012-3-9','钢材','元钢','Q235','A',24,600
union all select '2012-3-5','钢材','元钢','316','B',25,1000
union all select '2012-3-11','钢材','元钢','304','A',26,500
SELECT * FROM FLOATDATA
;with a
as(
select date
,'总值' as '项目'
,SUM(case when provider='A' and quality='q235' then amount else null end)as 'Aq235'
,SUM(case when provider='A' and quality='304' then amount else null end)as 'A304'
,SUM(case when provider='A' and quality='316' then amount else null end)as 'A316'
,SUM(case when provider='B' and quality='q235' then amount else null end)as 'Bq235'
,SUM(case when provider='B' and quality='304' then amount else null end)as 'B304'
,SUM(case when provider='A' and quality='316' then amount else null end)as 'B316'
from floatData
GROUP BY date),
b
as(
select date
,'重量'as '项目'
,SUM(case when provider='A' and quality='q235' then weight else null end)as 'Aq235'
,SUM(case when provider='A' and quality='304' then weight else null end)as 'A304'
,SUM(case when provider='A' and quality='316' then weight else null end)as 'A316'
,SUM(case when provider='B' and quality='q235' then weight else null end)as 'Bq235'
,SUM(case when provider='B' and quality='304' then weight else null end)as 'B304'
,SUM(case when provider='A' and quality='316' then weight else null end)as 'B316'
from floatData
GROUP BY date
),
c
as
(SELECT a.date,
'均值' as '项目'
,case when a.Aq235 is not null then a.Aq235/b.Aq235 end as 'Aq235'
,case when a.A304 is not null then a.A304/b.A304 end as 'A304'
,case when a.A316 is not null then a.A316/b.A316 end 'A316'
,case when a.Bq235 is not null then a.Bq235/b.Bq235 end 'Bq235'
,case when a.B304 is not null then a.B304/b.B304 end 'B304'
,case when a.B316 is not null then a.B316/b.B316 end 'B316'
from a left join b on a.date=b.date
)
select * from (
select * from B
UNION ALL SELECT * FROM A
UNION ALL SELECT * from c) as d
order by 1;
2013年03月28日 09点03分
6