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

解决方案 »

  1.   

    举例:create table #a(部门 varchar(100),种类 varchar(100),数量 int)
    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
      

  2.   

    select 车类名称,count(*) as 车道1 from t_表 group by 车类名称 where 车道号 = 1
    left join
    select count(*) as 车道2 from t_表 group by 车类名称 where 车道号 = 2
    ......
      

  3.   

    对不起!忘记一句话select 车类名称,count(*) as 车道1 from t_表 a group by 车类名称 where 车道号 = 1
    left join
    select count(*) as 车道2 from t_表 b group by 车类名称 where 车道号 = 2 on a.车类名称 = b.车类名称......
      

  4.   

    --交叉表
    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 车类名称')
      

  5.   

    --如果车道号很多,要改用下面的方法,防止生成的字符串超过8000,从而出错declare @sqlhead varchar(8000),@sqlend varchar(8000)
    ,@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 #
      

  6.   

    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+' into 你要的新表 from 你的表 group by 车类名称'exec(@sql)
    goselect * from 你要的新表
      

  7.   

    在VB中要把这个表放到DATAREPORT中去打印请问又该怎么做了.
      

  8.   

    这张表就是查询出来的结果请问pengdali(大力 V3.0) 上面的SQL程序看不懂能解释一下吗?对了我是想把查询的结果放在DATAREPORT中去打印,这段程序放在VB程序的哪里了?
      

  9.   

    sunshareforever(阳光)你的程序运行时 提示left join附近有错了谁知道错在哪里吗?
      

  10.   

    select  车类,
    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
      

  11.   

    zhangzhaoh(zzh) 没用呀,数量 是什么意思呀。
      

  12.   

    select 车道号,车类,SUM(车类数量),SUM(费用)
    FROM 收费记录表
    GROUP BY 车道号,车类
      

  13.   

    你用的什么数据库?可用交叉表或游标解决的.MYQQ57143540