insert into table2 (select * from table1)
解决方案 »
- 一个简单的存储过程,报错: Must declare the variable '@sTableName'。不知错在哪里?
- 请教关于insert的SQL语句?
- 查询只有一条记录的数据库,cpu占用率100%
- 关于数据库如何选择
- 这个是什么错误,谢谢大家,请帮忙~
- SQL 2000 升级到 sql 2008R2 64位
- 大家看看这个数据库建立后,ip uid pwd 应该是什么?
- 关于Excel操作,too easy!!!急,在线等待!
- 急急急:对数据库进行了错误的sql更新,能不能恢复到更新前的状态啊。。。
- because the 'primary' filegroup is full
- 请教两张表关联问题!
- ADO.NET里有个事务类,那请问在ADO中有没有这样的类?
set @sql='select 物料'
select @sql=@sql+',[部门'+cast(部门 as varchar)+'数量]=
isnull(sum(case when 部门='''+cast(部门 as varchar)+''' then 数量 end),0)
from table1
group by 部门
select @sql=@sql+' from table1 group by 物料'
exec(@sql)
insert @tab values('001',1,'01')
insert @tab values('001',4,'02')select 物料,[部门01数量]=sum(case when 部门='01' then 数量 else 0 end),
[部门02数量]=sum(case when 部门='02' then 数量 else 0 end) from @tab
group by 物料
[部门02数量]=isnull(sum(case when 部门='02' then 数量 end),0)
from table1
group by 物料
isnull(sum(case when 部门='02' then 数量 end),0) as 部门02数量
from table1
group by 物料
create table table1(物料 varchar(20),数量 int,部门 varchar(20))
insert table1 values('001',1,'01')
insert table1 values('001',4,'02')
go
select 物料,
(select 数量 from table1 where 物料=t.物料 and
部门='01')as 部门01数量,
(select 数量 from table1 where 物料=t.物料 and
部门='02')as 部门02数量
into #table2
from table1 t
group by 物料
go
select * from #table2
drop table #table2