转置一些SQL Server表列

时间:2015-11-04 14:51:20

标签: sql-server pivot

我需要一些真正的帮助。我有下表。我在Temp表中创建了它,如下所示。

--drop table ##DerivedTBL 
CREATE TABLE ##DerivedTBL (M_NO  INT, Location VARCHAR(20),SetTarget MONEY,TMonth VARCHAR(20)
            ,TargetAchieved Money  ,Total INT)

INSERT INTO ##DerivedTBL VALUES ('1', 'BRS', '0.7', 'Apr-15', '0.60465116279', '86')
INSERT INTO ##DerivedTBL VALUES ('1', 'WES', '0.7', 'Apr-15', '1', '6')
INSERT INTO ##DerivedTBL VALUES ('2', 'WES', '0.7', 'May-15', '0.5', '4')
INSERT INTO ##DerivedTBL VALUES ('2', 'BRS', '0.7', 'May-15', '0.595238095238', '84')
INSERT INTO ##DerivedTBL VALUES ('3', 'BRS', '0.7', 'Jun-15', '0.56862745098', '102')
INSERT INTO ##DerivedTBL VALUES ('3', 'WES', '0.7', 'Jun-15', '1', '9')
INSERT INTO ##DerivedTBL VALUES ('4', 'WES', '0.7', 'Jul-15', '0.666666666666', '15')
INSERT INTO ##DerivedTBL VALUES ('4', 'BRS', '0.7', 'Jul-15', '0.615384615384', '78')
INSERT INTO ##DerivedTBL VALUES ('5', 'BRS', '0.7', 'Aug-15', '0.47193877551', '392')
INSERT INTO ##DerivedTBL VALUES ('5', 'WES', '0.7', 'Aug-15', '0.4375', '16')
INSERT INTO ##DerivedTBL VALUES ('6', 'BRS', '0.7', 'Sep-15', '0.452830188679', '583')
INSERT INTO ##DerivedTBL VALUES ('6', 'WES', '0.7', 'Sep-15', '0.714285714285', '14')
INSERT INTO ##DerivedTBL VALUES ('7', 'BRS', '0.7', 'Oct-15', '0.475285171102', '526')
INSERT INTO ##DerivedTBL VALUES ('7', 'WES', '0.7', 'Oct-15', '0.5', '2')
INSERT INTO ##DerivedTBL VALUES ('8', 'BRS', '0.7', 'Nov-15', '1', '1')                             


select * from   ##DerivedTBL    

我想要实现的是转置(TMonth,TargetAchieved,Total)colums,以便结果看起来像下面的那个。 非常感谢提前

M_NO    Target  MonthYear1  MonthYear2  Total1  Total2
1   0.7 Apr-15  Apr-15  6   86
2   0.7 May-15  May-15  44  4
3   0.7 Jun-15  Jun-15  9   102
4   0.7 Jul-15  Jul-15  78  15
5   0.7 Aug-15  Aug-15  16  392

2 个答案:

答案 0 :(得分:1)

喜欢这个吗?

select A.M_NO, A.SetTarget, A.TMonth MonthYear1, B.TMonth MonthYear2, A.Total Total1, B.Total Total2 
   from   ##DerivedTBL A 
     INNER JOIN ##DerivedTBL B  ON A.M_NO = B.M_NO 
       AND A.Location ='WES' AND B.Location = 'BRS'

答案 1 :(得分:1)

如果每一行都有两个位置,那么您可以使用CASE表达式将行转换为列,否则您可以使用具有排名功能的UNPIVOT运算符,然后使用CASE表达式将行转换为如下列:

WITH Unpivoted
AS
(
    SELECT M_NO, TargetAchieved,  col, val, rn
    FROM 
    (
        SELECT
          M_NO,
          CAST(TMonth AS NVARCHAR(50)) AS TMonth, 
          CAST(Total AS NVARCHAR(50)) AS Total,
          TargetAchieved, 
          ROW_NUMBER() OVER(PARTITION BY M_NO ORDER BY location) AS RN 
        FROM DerivedTBL 
    ) AS t
    UNPIVOT
    (
        val
        FOR col IN(Total, TMonth)
    ) AS p
)
SELECT 
  M_NO,
  MAX(TargetAchieved) AS TargetAchieved,
  MAX(CASE WHEN col = 'TMonth' AND rn = 1 THEN val END) AS MonthYear1,
  MAX(CASE WHEN col = 'TMonth' AND rn = 2 THEN val END) AS MonthYear2,
  MAX(CASE WHEN col = 'Total' AND rn = 1 THEN val END) AS Total1,
  MAX(CASE WHEN col = 'Total' AND rn = 2 THEN val END) AS Total2
FROM Unpivoted AS u
GROUP BY M_NO;

这会给你这样的东西:

enter image description here