create table tb(fid varchar(10),字段1 varchar(10),字段2 int)
insert into tb select '1','aa',16
union all select '2','bb',17
union all select '3','cc',18
union all select '4','dd',19
declare @sql varchar(8000)
set @sql='select fid'
select @sql=@sql+',['+字段1+']=sum(case 字段1 when '''+字段1+''' then 字段2 else null end)' from tb group by 字段1
exec(@sql+' from tb group by fid')drop table tb
insert into tb select '1','aa',16
union all select '2','bb',17
union all select '3','cc',18
union all select '4','dd',19
declare @sql varchar(8000)
set @sql='select fid'
select @sql=@sql+',['+字段1+']=sum(case 字段1 when '''+字段1+''' then 字段2 else null end)' from tb group by 字段1
exec(@sql+' from tb group by fid')drop table tb
解决方案 »
- 求教子查询的写法?
- 100分买知识,几个名词解释
- 关于时间类型的转换小问题,马上结帖。
- 有没有什么工具,可以把字段的描述导出来的?
- SQl Server一个简单的问题请给位大侠帮忙,十分感谢!(在线等谢谢~!!)
- 在SQL Server里找不到"Microsoft Query"客户端应用程序?
- sql如何把一个字段设置为选择列
- 将datetime类型变成varchar后,日期全变成了sep 5 1999之类的,如何变回1999-09-09?
- about级联删除
- 求救:用delete将表中数据删除后,可以恢复吗?
- SQL注册用户
- 在sql 2005 里建立的数据库,然后在sql 2000里附加数据库时出现如下提示
insert into tb select '1','aa',16
union all select '2','bb',17
union all select '3','cc',18
union all select '4','dd',19
union all select '1','aa',20
declare @sql varchar(8000)
set @sql='select isnull(fid,''合计'')'
select @sql=@sql+',['+字段1+']=sum(case 字段1 when '''+字段1+''' then 字段2 else null end)' from tb group by 字段1
exec(@sql+' from tb group by fid with rollup')drop table tb
--这样?