在SQL Server中并排显示列中的行数据

时间:2019-07-09 06:15:57

标签: sql-server sql-server-2012

我有一个这样的表org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'downloadFilesProperties': Could not bind properties to DownloadFilesProperties (prefix=DownloadFiles, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'DirID[256]' of bean class [com.ldcc.cas.properties.DownloadFilesProperties$$EnhancerBySpringCGLIB$$5cd669a]: Invalid list index in property path 'DirID[256]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256

EmployeeObjective

我想像下面一样显示上表

EmployeeID  Objective   Weightage 
----------------------------------
1             A            20
1             B            20
1             C            20
1             D            20
1             E            20

可以使用EmployeeID Objective1 Objective1Weightage Objective2 Objective2Weightage......to Objective10 Objective10Weightage 1 A 20 B 20 ............. Empty Empty 或其他任何方式完成此操作吗?

3 个答案:

答案 0 :(得分:0)

select 
        EmployeeID,

        objective1 = max(obj1),
        weightage1 = MAX(weig1),
        objective2 = max(obj2),
        weightage2 = MAX(weig2),
        objective3 = max(obj3),
        weightage3 = MAX(weig3),
        objective4 = max(obj4),
        weightage4 = MAX(weig4),
        objective5 = max(obj5),
        weightage5 = MAX(weig5)
            from
    (
    select
        EmployeeID,
        Objective,
        Weightage,   
        'obj'+cast(row_number() over (partition by EmployeeID order by EmployeeID) as varchar(50)) as O,
        'weig'+cast(row_number() over (partition by EmployeeID order by EmployeeID) as varchar(50)) as W
    FROM table_name)temp
        pivot(max(Objective) for O in (obj1, obj2, obj3, obj4, obj5)) PivO
        pivot(max(Weightage) for W in (weig1, weig2, weig3, weig4, weig5)) PivW

    group by EmployeeID

答案 1 :(得分:0)

以下查询应执行您想要的操作:

CREATE TABLE #emp (EmployeeID  INT, Objective VARCHAR(10), Weightage INT)
INSERT INTO #emp VALUES
(1,'A',20),
(1,'B',20),
(1,'C',20),
(1,'D',20),
(1,'E',20)

;WITH CTE AS (
SELECT EmployeeID,
    CONVERT(VARCHAR(100), Objective) AS Objective,
    CONVERT(VARCHAR(100), Weightage) AS Weightage,
    ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY (SELECT 1)) rno 
FROM #emp )

SELECT * FROM (

SELECT EmployeeID, Val, Col+CAST(rno AS VARCHAR(20)) AS Col
FROM CTE
UNPIVOT(Val FOR Col IN (Objective,Weightage) ) unpiv ) a
PIVOT (
MAX(Val) FOR Col IN ([Objective1],[Weightage1],[Objective2],[Weightage2],[Objective3],[Weightage3],[Objective4],[Weightage4],[Objective5],[Weightage5])) piv

答案 2 :(得分:0)

最后,我设法通过以下查询获取输出。

DECLARE @T TABLE
    (
    EmployeeID INT NOT NULL 
    ,Objective NVARCHAR(250)
    , ObjectiveWeightage DECIMAL(6,4) NOT NULL
    )

INSERT INTO @T
(EmployeeID, Objective, ObjectiveWeightage)
   VALUES (1, 'A', 5 )
        , (1, 'B', 10   )
        , (1, 'C', 15   )
        , (1, 'D', 20   )
        , (1, 'E', 5    )
        , (1, 'F', 51   )
;WITH CTE_Rank AS
    (
    SELECT EmployeeID
        , Objective
        , ObjectiveWeightage 
        , sObjective = 'Objective' + CAST(DENSE_RANK() OVER (PARTITION BY EmployeeID ORDER BY Objective) AS VARCHAR(10))
        , sObjectiveWeightage  = 'ObjectiveWeightage' + CAST(DENSE_RANK() OVER (PARTITION BY EmployeeID ORDER BY Objective) AS VARCHAR(10))
    FROM @T
    )

SELECT EmployeeID 
    ,MAX( Objective1) Objective1 
    ,MAX( ObjectiveWeightage1)ObjectiveWeightage1
    ,MAX( Objective2         )Objective2 
    ,MAX( ObjectiveWeightage2)ObjectiveWeightage2
    ,MAX( Objective3         )Objective3
    ,MAX( ObjectiveWeightage3)ObjectiveWeightage3
    ,MAX( Objective4         )Objective4
    ,MAX( ObjectiveWeightage4)ObjectiveWeightage4
    ,MAX( Objective5         )Objective5
    ,MAX( ObjectiveWeightage5)ObjectiveWeightage5
    ,MAX( Objective6         )Objective6
    ,MAX( ObjectiveWeightage6)ObjectiveWeightage6
    ,MAX( Objective7         )Objective7
    ,MAX( ObjectiveWeightage7)ObjectiveWeightage7
    ,MAX( Objective8         )Objective8
    ,MAX( ObjectiveWeightage8)ObjectiveWeightage8
    ,MAX( Objective9         )Objective9
    ,MAX( ObjectiveWeightage9)ObjectiveWeightage9
    ,MAX( Objective10        )Objective10
    ,MAX( ObjectiveWeightage10)ObjectiveWeightage10
FROM CTE_Rank AS R
    PIVOT(MAX(Objective) FOR sObjective IN ([Objective1], [Objective2], [Objective3], [Objective4],[Objective5],[Objective6],[Objective7],[Objective8],[Objective9],[Objective10])) AS EmployeeObjective
    PIVOT(MAX(ObjectiveWeightage) FOR sObjectiveWeightage IN (ObjectiveWeightage1, ObjectiveWeightage2, ObjectiveWeightage3, ObjectiveWeightage4,ObjectiveWeightage5,ObjectiveWeightage6,ObjectiveWeightage7,ObjectiveWeightage8,ObjectiveWeightage9,ObjectiveWeightage10)) AS EmployeeObjectiveWeightage
GROUP BY EmployeeID