现通过连接查询查询出结果 
entityname    fzr
线路电气      李瓜
线路结构      王威
电气一次      肖飞
技经      尹豫落
电气二次      李选正
通信      许杰需要这种效果
n1       e1             n2            e2            n3          n3
李瓜     线路电气       王威          线路结构      肖飞        电气一次      ----------------------------依次显示。因为不是静态的,而entityname 有未知的个数。。fzr 对应entityname 
 求各位大侠帮帮小弟!

解决方案 »

  1.   

    两个两个一组,还是...?最好给出完整的表结构,测试数据,计算方法和正确结果.发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  2.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([entityname] varchar(8),[fzr] varchar(6))
    insert [tb]
    select '线路电气','李瓜' union all
    select '线路结构','王威' union all
    select '电气一次','肖飞' union all
    select '技经','尹豫落' union all
    select '电气二次','李选正' union all
    select '通信','许杰'select identity(int,1,1) as id,* into #temp from [tb]declare @sql varchar(8000)select @sql = isnull(@sql,'')+',max(case id when '+ltrim(id)+' then fzr else '''' end) as [n'+ltrim(id)+']
    ,max(case id when '+ltrim(id)+' then entityname else '''' end) as [e'+ltrim(id)+']'
    from #tempselect @sql = 'select '+stuff(@sql,1,1,'')+' from #temp'exec(@sql)drop table #temp
    -----------------------
    李瓜 线路电气 王威 线路结构 肖飞 电气一次 尹豫落 技经 李选正 电气二次 许杰 通信
      

  3.   

    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([entityname] varchar(8),[fzr] varchar(6))
    insert [tb]
    select '线路电气','李瓜' union all
    select '线路结构','王威' union all
    select '电气一次','肖飞' union all
    select '技经','尹豫落' union all
    select '电气二次','李选正' union all
    select '通信','许杰'select identity(int,1,1) as id,* into #temp from [tb]declare @sql varchar(8000)select @sql = isnull(@sql,'')+',max(case id when '+ltrim(id)+' then fzr else '''' end) as [n'+ltrim(id)+']
    ,max(case id when '+ltrim(id)+' then entityname else '''' end) as [e'+ltrim(id)+']'
    from #tempselect @sql = 'select '+stuff(@sql,1,1,'')+' from #temp'exec(@sql)drop table #temp
    -----------------------
    李瓜    线路电气    王威    线路结构    肖飞    电气一次    尹豫落    技经    李选正    电气二次    许杰    通信