declare @sql varchar(8000)
set @sql = 'select 单位名称,采样时间,采样地点'
select @sql = @sql + ',sum(case 项目 when '''+项目+''' then 结果值 else 0 end) as '+项目
from (select distinct 项目 from temp) as a
select @sql = @sql+' from temp group by 单位名称,采样时间,采样地点''exec(@sql)
set @sql = 'select 单位名称,采样时间,采样地点'
select @sql = @sql + ',sum(case 项目 when '''+项目+''' then 结果值 else 0 end) as '+项目
from (select distinct 项目 from temp) as a
select @sql = @sql+' from temp group by 单位名称,采样时间,采样地点''exec(@sql)
简单的说,全部当成字符,先设一个项目表,存放项目标头,然后强行假定最大40个项目(视纸张大小而定),再在存储过程中建一个有单位名称 采样时间 采样地点+40个字段的临时表#a,再先
insert into #a
values( '单位名称', '采样时间', '采样地点',''..... )
insert into #a
select distinct 单位名称 采样时间 采样地点,''..... from table
再用40个update 去update
如果前台是PB的话,PB有一个专门处理交叉表的数据窗口。
insert into temp values ('纸厂', '5、24', '处理前', '色度', 4)
insert into temp values ('纸厂', '5、24', '处理后','色度' ,6)
insert into temp values ('纸厂', '5、24', '处理前' ,'PH' ,6)
insert into temp values ('纸厂', '5、24', '处理后' ,'PH' ,9)
insert into temp values ('纸厂', '5、24', '处理前' ,'SS' ,50)
insert into temp values ('纸厂', '5、24', '处理后' ,'SS' ,180)
insert into temp values ('纸厂', '5、24', '处理前' ,'Hg' ,0.5)
insert into temp values ('纸厂', '5、24', '处理后' ,'Hg' ,0.1)
insert into temp values ('玻璃厂', '5、24', '处理前','色度', 5)
insert into temp values ('玻璃厂', '5、24', '处理后' ,'色度' ,6)
insert into temp values ('玻璃厂', '5、24', '处理前' ,'PH' ,5)
insert into temp values ('玻璃厂', '5、24', '处理后', 'PH' ,9)
insert into temp values ('玻璃厂', '5、24', '处理前' ,'SS' ,60)
insert into temp values ('玻璃厂', '5、24', '处理后','SS' ,185)
insert into temp values ('玻璃厂', '5、24', '处理前' ,'Hg' ,0.6)
insert into temp values ('玻璃厂', '5、24','处理后', 'Hg' ,0.15)
insert into temp values ('玻璃厂', '5、24 ','处理前' ,'Ass' ,0.665)
insert into temp values ('玻璃厂', '5、24 ','处理后' ,'Ass' ,0.045)
insert into temp values ('玻璃厂', '5、24 ','处理前' ,'Zn' ,0.76)
insert into temp values ('玻璃厂', '5、24 ','处理后' ,'Zn' ,0.045)
insert into temp values ('玻璃厂', '5、24 ','处理前' ,'油类' ,0.78)
insert into temp values ('玻璃厂', '5、24','处理后', '油类 ',0.077)
insert into temp values ('玻璃厂', '7、4 ','处理前', '色度 ',4)
insert into temp values ('玻璃厂', '7、4 ','处理后', '色度' ,7)
insert into temp values ('玻璃厂', '7、4 ','处理前', 'PH' ,5)
insert into temp values ('玻璃厂', '7、4 ','处理后', 'PH' ,7)
insert into temp values ('药厂', '5、24' ,'总排口', '色度' ,7)
insert into temp values ('药厂', '5、24 ','总排口', 'PH' ,6)
insert into temp values ('药厂', '5、24 ','总排口' ,'SS' ,200)
insert into temp values ('药厂', '5、24 ','总排口' ,'氨氮', 0.463)
insert into temp values ('药厂', '5、24 ','总排口' ,'硝酸根', 0.34)
declare @sql varchar(8000)
set @sql = 'select 单位名称,采样时间,采样地点'
select @sql = @sql + ',sum(case 项目 when '''+项目+''' then 结果值 else 0 end) as '+项目
from (select distinct 项目 from temp) as a
select @sql = @sql+' from temp group by 单位名称,采样时间,采样地点'exec(@sql)drop table temp
色度 硝酸根 油类
玻璃厂 5、24 处理后 .000 .150 9.000 185.000 .000 .000 6.000 .000 .077
玻璃厂 5、24 处理前 .000 .600 5.000 60.000 .000 .000 5.000 .000 .000
玻璃厂 5、24 处理后 .045 .000 .000 .000 .045 .000 .000 .000 .000
玻璃厂 5、24 处理前 .665 .000 .000 .000 .760 .000 .000 .000 .780
玻璃厂 7、4 处理后 .000 .000 7.000 .000 .000 .000 7.000 .000 .000
玻璃厂 7、4 处理前 .000 .000 5.000 .000 .000 .000 4.000 .000 .000
药厂 5、24 总排口 .000 .000 .000 .000 .000 .000 7.000 .000 .000
药厂 5、24 总排口 .000 .000 6.000 200.000 .000 .463 .000 .340 .000
纸厂 5、24 处理后 .000 .100 9.000 180.000 .000 .000 6.000 .000 .000
纸厂 5、24 处理前 .000 .500 6.000 50.000 .000 .000 4.000 .000 .000
玻璃厂5月24日的数据能否按采样地点不重复的汇总成两行数据
即同一日期只汇总成一行数据感谢各位高手给予如此详细的答复
insert into temp values ('纸厂', '5、24', '处理前', '色度', 4)
insert into temp values ('纸厂', '5、24', '处理后','色度' ,6)
insert into temp values ('纸厂', '5、24', '处理前' ,'PH' ,6)
insert into temp values ('纸厂', '5、24', '处理后' ,'PH' ,9)
insert into temp values ('纸厂', '5、24', '处理前' ,'SS' ,50)
insert into temp values ('纸厂', '5、24', '处理后' ,'SS' ,180)
insert into temp values ('纸厂', '5、24', '处理前' ,'Hg' ,0.5)
insert into temp values ('纸厂', '5、24', '处理后' ,'Hg' ,0.1)
insert into temp values ('玻璃厂', '5、24', '处理前','色度', 5)
insert into temp values ('玻璃厂', '5、24', '处理后' ,'色度' ,6)
insert into temp values ('玻璃厂', '5、24', '处理前' ,'PH' ,5)
insert into temp values ('玻璃厂', '5、24', '处理后', 'PH' ,9)
insert into temp values ('玻璃厂', '5、24', '处理前' ,'SS' ,60)
insert into temp values ('玻璃厂', '5、24', '处理后','SS' ,185)
insert into temp values ('玻璃厂', '5、24', '处理前' ,'Hg' ,0.6)
insert into temp values ('玻璃厂', '5、24','处理后', 'Hg' ,0.15)
insert into temp values ('玻璃厂', '5、24 ','处理前' ,'Ass' ,0.665)
insert into temp values ('玻璃厂', '5、24 ','处理后' ,'Ass' ,0.045)
insert into temp values ('玻璃厂', '5、24 ','处理前' ,'Zn' ,0.76)
insert into temp values ('玻璃厂', '5、24 ','处理后' ,'Zn' ,0.045)
insert into temp values ('玻璃厂', '5、24 ','处理前' ,'油类' ,0.78)
insert into temp values ('玻璃厂', '5、24','处理后', '油类 ',0.077)
insert into temp values ('玻璃厂', '7、4 ','处理前', '色度 ',4)
insert into temp values ('玻璃厂', '7、4 ','处理后', '色度' ,7)
insert into temp values ('玻璃厂', '7、4 ','处理前', 'PH' ,5)
insert into temp values ('玻璃厂', '7、4 ','处理后', 'PH' ,7)
insert into temp values ('药厂', '5、24' ,'总排口', '色度' ,7)
insert into temp values ('药厂', '5、24 ','总排口', 'PH' ,6)
insert into temp values ('药厂', '5、24 ','总排口' ,'SS' ,200)
insert into temp values ('药厂', '5、24 ','总排口' ,'氨氮', 0.463)
insert into temp values ('药厂', '5、24 ','总排口' ,'硝酸根', 0.34)
declare @sql varchar(8000)
set @sql = 'select 单位名称,采样时间'
select @sql = @sql + ',sum(case when 采样地点=''处理前'' and 项目='''+项目+''' then 结果值 else 0 end) 处理前'+项目+',
sum(case when 采样地点=''处理后'' and 项目='''+项目+''' then 结果值 else 0 end) 处理后'+项目
from (select distinct 项目 from temp) as a
select @sql = @sql+' from temp group by 单位名称,采样时间'exec(@sql)
go
drop table temp
insert into temp values ('纸厂', '5、24', '处理前', '色度', 4)
insert into temp values ('纸厂', '5、24', '处理后','色度' ,6)
insert into temp values ('纸厂', '5、24', '处理前' ,'PH' ,6)
insert into temp values ('纸厂', '5、24', '处理后' ,'PH' ,9)
insert into temp values ('纸厂', '5、24', '处理前' ,'SS' ,50)
insert into temp values ('纸厂', '5、24', '处理后' ,'SS' ,180)
insert into temp values ('纸厂', '5、24', '处理前' ,'Hg' ,0.5)
insert into temp values ('纸厂', '5、24', '处理后' ,'Hg' ,0.1)
insert into temp values ('玻璃厂', '5、24', '处理前','色度', 5)
insert into temp values ('玻璃厂', '5、24', '处理后' ,'色度' ,6)
insert into temp values ('玻璃厂', '5、24', '处理前' ,'PH' ,5)
insert into temp values ('玻璃厂', '5、24', '处理后', 'PH' ,9)
insert into temp values ('玻璃厂', '5、24', '处理前' ,'SS' ,60)
insert into temp values ('玻璃厂', '5、24', '处理后','SS' ,185)
insert into temp values ('玻璃厂', '5、24', '处理前' ,'Hg' ,0.6)
insert into temp values ('玻璃厂', '5、24','处理后', 'Hg' ,0.15)
insert into temp values ('玻璃厂', '5、24 ','处理前' ,'Ass' ,0.665)
insert into temp values ('玻璃厂', '5、24 ','处理后' ,'Ass' ,0.045)
insert into temp values ('玻璃厂', '5、24 ','处理前' ,'Zn' ,0.76)
insert into temp values ('玻璃厂', '5、24 ','处理后' ,'Zn' ,0.045)
insert into temp values ('玻璃厂', '5、24 ','处理前' ,'油类' ,0.78)
insert into temp values ('玻璃厂', '5、24','处理后', '油类 ',0.077)
insert into temp values ('玻璃厂', '7、4 ','处理前', '色度 ',4)
insert into temp values ('玻璃厂', '7、4 ','处理后', '色度' ,7)
insert into temp values ('玻璃厂', '7、4 ','处理前', 'PH' ,5)
insert into temp values ('玻璃厂', '7、4 ','处理后', 'PH' ,7)
insert into temp values ('药厂', '5、24' ,'总排口', '色度' ,7)
insert into temp values ('药厂', '5、24 ','总排口', 'PH' ,6)
insert into temp values ('药厂', '5、24 ','总排口' ,'SS' ,200)
insert into temp values ('药厂', '5、24 ','总排口' ,'氨氮', 0.463)
insert into temp values ('药厂', '5、24 ','总排口' ,'硝酸根', 0.34)
declare @sql varchar(8000)
set @sql = 'select 单位名称,采样时间'
select @sql = @sql + ',sum(case when 采样地点=''处理前'' and 项目='''+项目+''' then 结果值 else 0 end) 处理前'+项目+',
sum(case when 采样地点=''处理后'' and 项目='''+项目+''' then 结果值 else 0 end) 处理后'+项目
from (select distinct 项目 from temp) as a
select @sql = @sql+' from temp group by 单位名称,采样时间'exec(@sql)drop table tempgo