id     pid             bm
7 2 单位1
8 2 单位22
9 2 单位3333
10 3 单位1
11 3 单位22
12 3 单位22
表里信息如上。我想实现下面的效果。----------
pid    bm1    bm2     bm3
2     单位1  单位22    单位333
3     单位1  单位22    单位22
这个怎么处理呢?
现在以上这个是静态的。假如我这里的bm会不断增加,有了单位444、单位555....那么又该怎么写呢?是sql语句还是存储过程?请教。

解决方案 »

  1.   

    create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
    insert into tb values('张三' , '语文' , 74)
    insert into tb values('张三' , '数学' , 83)
    insert into tb values('张三' , '物理' , 93)
    insert into tb values('李四' , '语文' , 74)
    insert into tb values('李四' , '数学' , 84)
    insert into tb values('李四' , '物理' , 94)
    select UserName,sum(case when Subject= '数学' then Score else 0 end) [数学],sum(case when Subject= '物理' then Score else 0 end) [物理],sum(case when Subject= '语文' then Score else 0 end) [语文]
    declare @sql varchar(1000)
    set @sql='select UserName'
    select @sql=@sql+',sum(case when Subject= ''' +Subject+ ''' then Score else 0 end) ['+Subject+']' from (select distinct Subject from tb)aset @sql = @sql + ' from tb group by UserName'
    print @sql
    exec(@sql) 
    --讲解:--这个是第一次执行
    select 姓名,max(case 课程 when '数学' then 分数 else 0 end) [数学], 
    --这个是第二次
    select 姓名,max(case 课程 when '数学' then 分数 else 0 end) [数学], max(case 课程 when '物理' then 分数 else 0 end) [物理] , 
    --这个是第三次
    select 姓名,max(case 课程 when '数学' then 分数 else 0 end) [数学], max(case 课程 when '物理' then 分数 else 0 end) [物理] , max(case 课程 when '语文' then 分数 else 0 end) [语文] 
    --这个的数量来自于
    (select distinct 课程 from tb)--这里只有3们课程create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    gocreate function dbo.f_str(@id int) returns varchar(100)
    as
    begin
        declare @str varchar(1000)
        set @str = ''
        select @str = @str + ',' + cast(value as varchar) from tb where id = @id
        set @str = right(@str , len(@str) - 1)
        return @str
    end
    go--调用函数
    select id , value = dbo.f_str(id) from tb group by iddrop function dbo.f_str
    drop table tb
    我只说一个地方 
    select @str = @str + ',' + cast(value as varchar) from tb where id = @id 
    你把这个看懂就明白了 
    例如当@id=1 
    select @str = @str + ',' + cast(value as varchar) from tb where id = 1 
    把满足id=1的str值通过','累加 
    当id是动态的就是1或者2...是当满足1的查询完了,把值付给str之后 
    在查询满足2的直到所有的ID完为止 
    这样明白了吧 
      

  2.   

    把我这个搞回了 
    就会啦 
    http://blog.csdn.net/ws_hgo/archive/2009/03/17/3999394.aspx
      

  3.   

    --> 测试数据: [s]
    if object_id('[s]') is not null drop table [s]
    create table [s] (id int,pid int,bm varchar(8))
    insert into [s]
    select 7,2,'单位1' union all
    select 8,2,'单位22' union all
    select 9,2,'单位3333' union all
    select 10,3,'单位1' union all
    select 11,3,'单位22' union all
    select 12,3,'单位22'declare @sql varchar(1000)
    set @sql='select pid'
    select @sql=@sql+',[bm'+ltrim(px)+']=max(case px when '''+ltrim(px)+''' then bm else '''' end)'
    from (select distinct px=(select count(1) from s where pid=a.pid and id<=a.id) from [s] a)a
    set @sql=@sql+' from (select px=(select count(1) from s where pid=a.pid and id<=a.id),* from [s] a)a group by pid'
    exec(@sql)