declare @sql varchar(8000)
set @sql = 'select 车类名称'
select @sql = @sql + ',sum(case 车道号 when '''+cast(车道号 as varchar(10))+''' then 数量 else 0 end) [车道号'+cast(车道号 as varchar(10))+']'
from (select distinct 车道号 from 你的表) as a
select @sql = @sql+' from 你的表 group by 车类名称'exec(@sql)
go
set @sql = 'select 车类名称'
select @sql = @sql + ',sum(case 车道号 when '''+cast(车道号 as varchar(10))+''' then 数量 else 0 end) [车道号'+cast(车道号 as varchar(10))+']'
from (select distinct 车道号 from 你的表) as a
select @sql = @sql+' from 你的表 group by 车类名称'exec(@sql)
go
解决方案 »
- 【触发器】第一次动手写触发器
- 关于SQL表结构升级
- 同一张表两列值互换的问题
- 混合模式论证的SQL SERVER为什么还非要先通过Windows认证才能访问数据库?
- 请教: 此SQL输出结果是什么?
- 3个表where 语句接join
- sql里面视图模式下如何对表进行创建索引
- 有请各位大虾赐教
- 我以前一直用ASP+ACCESS ,现在要用SQL了,请问是不是只要安装SQL2000 SERVER 就可以建立本地SQL调试环境了,调试好的SQL数据库要上传的
- 我在SQL SERVER中写了个作业来定时备份数据库,必须要启动SQL SERVER Agent服务吗?启动了这个服务后跟平常的SQL SERBER 服务有什么区别
- oracle语句可以和sql server200通用吗,有些oracle语句在sql server中好象不好用?
- 菜鸟分数大派送:sql服务器连接问题
insert #a values('n1','aa',11)
insert #a values('n1','ba',11)
insert #a values('n2','bb',1)
insert #a values('n2','aa',45)
insert #a values('n3','cc',81)
insert #a values('n3','aa',11)
insert #a values('n3','ba',561)
insert #a values('n3','bb',14)declare @sql varchar(8000)
set @sql = 'select 部门'
select @sql = @sql + ',sum(case 种类 when '''+种类+''' then 数量 else 0 end) ['+种类+'数量]'
from (select distinct 种类 from #a) aselect @sql =@sql+' from #a group by 部门'exec(@sql)drop table #a
left join
select count(*) as 车道2 from t_表 group by 车类名称 where 车道号 = 2
......
left join
select count(*) as 车道2 from t_表 b group by 车类名称 where 车道号 = 2 on a.车类名称 = b.车类名称......
declare @sql varchar(8000)
set @sql='select 车类名称'
select @sql=@sql+',['+车道号+']=sum(case 车道号 when '''+车道号+''' then 数量 end)' from (select distinct 车道号 from 表) a
exec(@sql+' from 表 group by 车类名称')
,@sql1 varchar(8000),@sql2 varchar(8000),@sql3 varchar(8000),@sql4 varchar(8000)
,@i int,@ic varchar(20)--生成数据处理临时表
select id=identity(int,0,1),gid=0
,a=',['+车道号+']=sum(case 车道号 when '''
+车道号+''' then 数量 else 0 end)'
into # from(select distinct 车道号 from 表) a--判断需要多少个变量来处理
select @i=max(len(a)) from #
set @i=7800/@i--分组临时表
update # set gid=id/@i
select @i=max(gid) from #--生成数据处理语句
select @sqlhead='''select 车类名称'''
,@sqlend=''' from 表 group by 车类名称'''
,@sql1='',@sql2='select ',@sql3='',@sql4=''while @i>=0
select @ic=cast(@i as varchar),@i=@i-1
,@sql1='@'+@ic+' varchar(8000),'+@sql1
,@sql2=@sql2+'@'+@ic+'='''','
,@sql3='select @'+@ic+'=@'+@ic+'+a from # where gid='+@ic
+char(13)+@sql3
,@sql4=@sql4+',@'+@icselect @sql1='declare '+left(@sql1,len(@sql1)-1)+char(13)
,@sql2=left(@sql2,len(@sql2)-1)+char(13)
,@sql3=left(@sql3,len(@sql3)-1)
,@sql4=substring(@sql4,2,8000)--执行
exec( @sql1+@sql2+@sql3+'
exec('+@sqlhead+'+'+@sql4+'+'+@sqlend+')'
)--删除临时表
drop table #
set @sql = 'select 车类名称'
select @sql = @sql + ',sum(case 车道号 when '''+cast(车道号 as varchar(10))+''' then 数量 else 0 end) [车道号'+cast(车道号 as varchar(10))+']'
from (select distinct 车道号 from 你的表) as a
select @sql = @sql+' into 你要的新表 from 你的表 group by 车类名称'exec(@sql)
goselect * from 你要的新表
sum(case when 车道号 = 值1 then 数量 else 0 end) 车道号1,
sum(case when 车道号 = 值2 then 数量 else 0 end) 车道号2,
sum(case when 车道号 = 值3 then 数量 else 0 end) 车道号3,
sum(case when 车道号 = 值4 then 数量 else 0 end) 车道号4,
........
from table
FROM 收费记录表
GROUP BY 车道号,车类