SQL服务器,使用行分区的行到列

时间:2017-03-12 14:45:51

标签: sql-server

我的结构如下......

人    - personid

  Skills
      - skillid
      - personid
      - skill

所以一个人会有很多技能。我的问题是我如何编写一个联接来显示技能列。

在过去的工作中,一位同事使用划分分区来实现这一目标,但我正在努力想办法。我知道一个支点是一个选项,但我的偏好是划分分区。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

如果最大技能数未知,则需要使用动态sql。您需要使用row_number()对按<div class="container"> <div class="row row-eq-height"> <div class="col-md-6 text-justify"> <div class="white-box"> <h5>SOME TITLE</h5> <p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. </p> </div> </div> <div class="col-md-6 text-justify"> <div class="white-box"> <h5>SOME TITLE</h5> <p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. </p> <p><a href="#" class="btn btn-primary btn-outline btn-sm">Continued &gt;&gt;</a></p> </div> </div> </div> </div> 分区的每个列表进行编号,以便与pivot()一起使用。

测试设置:

PersonId

rextester演示:http://rextester.com/OFHO71002

代码生成:

create table t (skillid int, personid int, skill varchar(32));
insert into t values (1,1,'sql-server'),(3,1,'sql'),(9,2,'sql-server');

declare @cols nvarchar(max);
declare @sql  nvarchar(max);

  select @cols = stuff((
    select distinct 
      ',' + quotename('Skill_'
          +convert(nvarchar(10),row_number() over (
              partition by PersonId 
              order by     Skill 
          ))
          )
      from t 
      for xml path (''), type).value('.','nvarchar(max)')
    ,1,1,'');

select @sql = '
 select PersonId, ' + @cols + '
  from  (
    select 
        PersonId
      , Skill
      , rn=''Skill_''+convert(nvarchar(10),row_number() over (
          partition by PersonId 
          order by     Skill 
          ))
      from t
      ) as a
 pivot (max([Skill]) for [rn] in (' + @cols + ') ) p';
 select @sql as CodeGenerated;
 exec sp_executesql @sql;

返回:

select PersonId, [Skill_1],[Skill_2]
  from  (
    select 
        PersonId
      , Skill
      , rn='Skill_'+convert(nvarchar(10),row_number() over (
          partition by PersonId 
          order by     Skill 
          ))
      from t
      ) as a
    pivot (max([Skill]) for [rn] in ([Skill_1],[Skill_2]) ) p