在MSSQL中的不同列中但在同一行中显示同一表中的数据

时间:2013-11-21 15:50:54

标签: sql sql-server tsql

我将表格设为enter image description here

我想要做的是选择像

这样的数据
OUTPUT{ID,FirstName,LastName,SkillONE,SkillOneExp,SkillTwo,SkillTwoExp,SkillThree,SkillThreeExp}

这里SkillOne将展示最有经验的技能(如java,C ++),SkillOneExp是该技能的exp年份等。这样只会显示3个。每位员工只有一行。

我试过

select 
e.ID
,e.FirstName
,e.LastName
,es.experience
,CAST
(
    CASE
        WHEN tf.rownumber = 1
        THEN tf.skill_description 
    END

    AS varchar(50)
)as SKILLONE
,CAST
(
    CASE
        WHEN tf.rownumber = 1
        THEN tf.experience 
    END

    AS varchar(50)
)as SKILLONE_EXP
,CAST
(
    CASE
        WHEN tf.rownumber = 2
        THEN tf.skill_description 
    END

    AS varchar(50)
)as SKILTWO
,CAST
(
    CASE
        WHEN tf.rownumber = 2
        THEN tf.experience 
    END

    AS varchar(50)
)as SKILLTWO_EXP
from
Employee e
JOIN
EmployeeSkill es
on es.ID=e.ID
JOIN 
skills s
on s.skill_ID=es.skill_ID

JOIN
(
 select * from 
 (  
    (select 
        ROW_NUMBER() OVER (ORDER BY es2.experience ASC) AS rownumber,
        es2.ID
        ,es2.skill_ID
        ,es2.experience
        ,s2.skill_description
        from
        EmployeeSkill es2
        JOIN
        skills s2
        on
        s2.skill_ID=es2.skill_ID

    ) 
) as tf2 
)as tf 
on tf.id= e.id

通过使用这个我没有得到正确的结果。我正在使用MS SQL 2012。

2 个答案:

答案 0 :(得分:0)

SELECT
  emp.ID,
  emp.FirstName,
  emp.LastName,
  MIN(CASE i WHEN 1 THEN ski.skill_description END) AS SkillOne     ,
  MIN(CASE i WHEN 1 THEN ski.experience        END) AS SkillOneExp  ,
  MIN(CASE i WHEN 2 THEN ski.skill_description END) AS SkillTwo     ,
  MIN(CASE i WHEN 2 THEN ski.experience        END) AS SkillTwoExp  ,
  MIN(CASE i WHEN 3 THEN ski.skill_description END) AS SkillThree   ,
  MIN(CASE i WHEN 3 THEN ski.experience        END) AS SkillThreeExp
FROM
  Employee AS emp
CROSS APPLY (
  SELECT TOP 3
    ROW_NUMBER() OVER(ORDER BY experience DESC) i,
    CAST(skill_description AS varchar(50),
    CAST(experience        AS varchar(50)
  FROM EmployeeSkill t1
  INNER JOIN skills t2
    ON (t1.skill_ID = t2.skill_ID)
  ORDER BY experience DESC
) AS ski
GROUP BY
  emp.ID,
  emp.FirstName,
  emp.LastName 

答案 1 :(得分:0)

通过@anon编辑查询我解决了我的问题。非常感谢他。

SELECT
emp.ID,
emp.FirstName,
emp.LastName,
MIN(CASE i WHEN 1 THEN ski.skill_description END) AS SkillOne     ,
MIN(CASE i WHEN 1 THEN ski.experience        END) AS SkillOneExp  ,
MIN(CASE i WHEN 2 THEN ski.skill_description END) AS SkillTwo     ,
MIN(CASE i WHEN 2 THEN ski.experience        END) AS SkillTwoExp  ,
MIN(CASE i WHEN 3 THEN ski.skill_description END) AS SkillThree   ,
MIN(CASE i WHEN 3 THEN ski.experience        END) AS SkillThreeExp
FROM
Employee AS emp
CROSS APPLY (
  SELECT TOP 3
  ROW_NUMBER() OVER(ORDER BY experience DESC) i,
  CAST(skill_description AS varchar(50) ) AS skill_description,
  CAST(experience        AS varchar(50) ) AS experience
FROM EmployeeSkill t1
INNER JOIN skills t2
ON (t1.skill_ID = t2.skill_ID) AND t1.ID=emp.ID
ORDER BY experience DESC
) AS ski
GROUP BY
emp.ID,
emp.FirstName,
emp.LastName