有这样的一个表:
项目 站名 值 时间
101 1 0.01 5:00
102 1 0.02 5:00
103 1 0.03 5:00
106 1 0.05 5:00
101 2 0.001 5:00
102 2 0.002 5:00
103 2 0.003 5:00
106 2 0.005 5:00我想 转换成这样的形式 并且写入 这张新的表
站名 101 102 103 106 时间
1 0.01 0.02 0.03 0.05 5:00
2 0.001 0.002 0.003 0.005 5:00
项目 站名 值 时间
101 1 0.01 5:00
102 1 0.02 5:00
103 1 0.03 5:00
106 1 0.05 5:00
101 2 0.001 5:00
102 2 0.002 5:00
103 2 0.003 5:00
106 2 0.005 5:00我想 转换成这样的形式 并且写入 这张新的表
站名 101 102 103 106 时间
1 0.01 0.02 0.03 0.05 5:00
2 0.001 0.002 0.003 0.005 5:00
解决方案 »
- 怎么样把自定义的一个结构体放入collection中啊!急...火急--火急急!
- 如何对控件进行全局子类化,也就是对所有的控件实例子类化?
- 谁能介绍一个系统讲述VB打印的地方啊
- 如何显示打印机设置
- 一个有关dll的问题
- ~~~~~~~~~~~~如何将VB写的程序生成安装程序?~~~~~~~~~~~~~~~
- 汉化问题,各位大虾帮帮忙啊!
- 为什么在ADO里面我的记录集不能移动到下一条???
- 创建EXCEL时添加SHEET时出现如下错误:?????
- 一个愚蠢的问题希望您有聪明的回答!
- adodc控件SLQ统计为空。怎么办???
- 奇怪,为什么将Caption地值设为A&B,显示的却是AB注'(B下有以下划线!)
create table 表(项目 int, 站名 int,值 varchar(10), 时间 varchar(4))
insert 表 select 101, 1 ,'0.01','5:00'
union all select 102 , 1 ,'0.02','5:00'
union all select 103 , 1 ,'0.03','5:00'
union all select 106 ,1 ,'0.05','5:00'
union all select 101 ,2 ,'0.001','5:00'
union all select 102 , 2 ,'0.002','5:00'
union all select 103 , 2 ,'0.003','5:00'
union all select 106, 2 ,'0.005','5:00'declare @sql varchar(8000)
set @sql = 'select 站名'
select @sql = @sql + ',max(case 项目 when '''+cast(项目 as char(3))+''' then 值 end) ['+cast(项目 as char(3))+']'
from (select distinct 项目 from 表) as a
select @sql = @sql+' ,时间 from 表 group by 站名,时间'
exec(@sql)站名 101 102 103 106 时间
----------- ---------- ---------- ---------- ---------- ----
1 0.01 0.02 0.03 0.05 5:00
2 0.001 0.002 0.003 0.005 5:00
[交流]行列转换
简单的SQL也能做做,但是实用性不强
select (case 项目 when '101' then SUM(值) end) as 101,(case 项目 when '102' then sum(值) end) as 102, ... ,'5:00' as 时间 from a group by 站名
因为项目名称可能是动态的
所以最好在此之前先
select distinct 项目 from a
然后使用for循环自动组成如上的SQL语句
例如:
sqlsa = sqlsa + "case 项目 when " + rs.fields("项目").value + "then SUM(值) end) as" + rs.fields("项目").value
这是在VB中的使用
可以,但是效率较低
建立存储过程可以更高的效率,把select distinct 项目 from a封装成一个数据库函数,然后在你的存储过程调用。
在存储过程中不要使用游标,效率很低而且极易引起内存溢出(大数据量统计)
select SUM(case 项目 when '101' then 值 end) as 101,SUM(case 项目 when '102' then 值 end) as 102, ... ,'5:00' as 时间 from a group by 站名
insert into test select '101', 1, 0.01, '5:00'
insert into test select '102', 1, 0.02, '5:00'
insert into test select '103', 1, 0.03, '5:00'
insert into test select '106', 1, 0.05, '5:00'
insert into test select '101', 2, 0.001, '5:00'
insert into test select '102', 2, 0.002, '5:00'
insert into test select '103', 2, 0.003, '5:00'
insert into test select '106', 2, 0.005, '5:00'declare @sql varchar(8000)
set @sql='select 站名'
select @sql= @sql + ',sum(case 项目 when '''+ 项目 + ''' then 值 else 0 end) as [' + 项目 +']'
from (select distinct 项目 from test) as a
select @sql=@sql+',时间 from test group by 站名,时间'
select @sql
exec(@sql)drop table test
insert into test select 101, 1, 0.01, '5:00'
insert into test select 102, 1, 0.02, '5:00'
insert into test select 103, 1, 0.03, '5:00'
insert into test select 106, 1, 0.05, '5:00'
insert into test select 101, 2, 0.001, '5:00'
insert into test select 102, 2, 0.002, '5:00'
insert into test select 103, 2, 0.003, '5:00'
insert into test select 106, 2, 0.005, '5:00'declare @sql varchar(8000)
set @sql='select 站名'
select @sql= @sql + ',sum(case 项目 when '''+ cast(项目 as varchar(5))+ ''' then 值 else 0 end) as [' + cast(项目 as varchar(5)) +']'
from (select distinct 项目 from test) as a
select @sql=@sql+',时间 from test group by 站名,时间'
select @sql
exec(@sql)drop table test
项目 站名 值 时间
项目 站名 是固定的数目
值跟 时间是变化的 一个小时一变化
我想 定义一个存储过程 跟一个JOB
每小时 自动 把数据导入到新表
参数是 时间..
我按上面的方法试了
可是 不好使啊...