比如  
 
姓名            特征            特征类型  
小明            很高            1  
小明            很胖            2  
小明            英语好            3  
小明            画画好            4  
小红            很高            1  
小红            画画好            4  
想实现搜索特征为"很高"并且"画画好"的同学,也即搜索结果为"小明"和"小红",并且横向排列其特征,应该如何进行,谢谢结果应为:      特征1  特征2  特征3    特征4
小明  很高   很胖   英语好   画画好
小红  很高   null   null     画画好

解决方案 »

  1.   

    前一段时间做过个类似的东西,不过最后我是在DataSet中解决的。
      

  2.   

    create table tb(姓名 varchar(10),特征 varchar(10),特征类型 int)
    insert into tb select '小明','很高',1
    union all select '小明','很胖',2
    union all select '小明','英语好',3
    union all select '小明','画画好',4
    union all select '小红','很高',1
    union all select '小红','画画好',4  
    goselect * from 
    (select 姓名,特征一=max(case when 特征类型=1 then 特征 end),特征二=max(case when 特征类型=2 then 特征 end),特征三=max(case when 特征类型=3 then 特征 end),特征四=max(case when 特征类型=4 then 特征 end)
    from tb group by 姓名)a
    where a.特征一='很高' and a.特征四='画画好'
      

  3.   

    请参考http://community.csdn.net/Expert/topic/4661/4661021.xml?temp=.5565454稍作改动即可
      

  4.   


    create table #t(
    姓名 varchar(20),
    特征 varchar(50),
    特征类型 int
    )
    go
    insert #t select '小明','很高',1 
    union select '小明','很胖',2
    union select '小明','英语好',3
    union select '小明',' 画画好 ',4
    union select '小红','很高',1
    union select '小红',' 画画好 ',4declare @sql varchar(8000)set @sql = 'select 姓名'
    select @sql = @sql+',max(case 特征类型 when '''+cast(特征类型 as varchar)+''' then 特征 else null end ) as 特征'+cast(特征类型 as varchar)
      from (select distinct 特征类型 from  #t)a order by 特征类型
     
    set @sql =@sql+' from #t group by 姓名'
    exec(@sql)
      

  5.   

    create table tb(姓名 varchar(10),特征 varchar(10),特征类型 int)
    insert tb select '小明','很高',1  
    union all select '小明','很胖',2  
    union all select '小明','英语好',3  
    union all select '小明','画画好',4  
    union all select '小红','很高',1  
    union all select '小红','画画好',4declare @sql varchar(8000)
    set @sql='select 姓名'
    select @sql=@sql+',[特征'+cast(特征类型 as varchar)+']=max(case 特征类型 when '''+cast(特征类型 as varchar)+''' then 特征 else '''' end)' from tb group by 特征类型
    exec(@sql+' from tb group by 姓名 order by 姓名 desc')drop table tb
      

  6.   

    改了一下:--测试数据
    create table tb(姓名 varchar(10),特征 varchar(10),特征类型 int)
    insert into tb select '小明','很高',1
    union all select '小明','很胖',2
    union all select '小明','英语好',3
    union all select '小明','画画好',4
    union all select '小红','很高',1
    union all select '小红','画画好',4  
    go--创建存储过程
    create proc sp_f
    @c1 varchar(10),
    @c2 varchar(10)
    as
    begin
     declare @s varchar(1000)
     set @s=''
     select @s='select 姓名'
     select @s=@s+',特征'+cast(特征类型 as varchar)+'=max(case when 特征类型='''+cast(特征类型 as varchar)+''' then 特征 end) ' from tb  group by 特征类型
     select @s=@s+'from tb where 姓名 in(select distinct a.姓名 from tb a,tb b where a.姓名=b.姓名 and a.特征='''+@c1+''' and b.特征='''+@c2+''')group by 姓名'
     exec(@s)
    end
    go
    --执行存储过程
    exec sp_f '很高','画画好'
    --删除垃圾信息
    drop table tb
    drop proc sp_f