SQL列作为行加入,PIVOT?

时间:2016-09-20 09:33:38

标签: sql-server join pivot rows

我尝试使用枢轴答案处理我在这里找到的结果,但我没有让它工作。我试图将一个表中的列连接到另一个表中的行按日期。

我有以下表格结构/结果。

假期表

  

日期| SusanCalladine | ClaireWillis | FazailaPirbhai
  2016-08-22 | 0 | 0 | 1
  2016-08-23 | 0 | 1 | 1
  2016-08-24 | 0 | 1 | 1
  2016-08-25 | 0 | 1 | 1
  2016-08-26 | 0 | 0 | 1个

我正在尝试加入日期和行到以下(每个日期有3个人分开的行)

电话表

  

CallDate | AgentName | InboundCallsAnswered | InboundHandleTime
  2016-08-22 |克莱尔威利斯| 0 | 0
  2016-08-22 | Fazaila Pirbhai | 36 | 17484个
  2016-08-22 | Susan Calladine | 0 | 0
  2016-08-23 |克莱尔威利斯| 12 | 4250
  2016-08-23 | Fazaila Pirbhai | 16 | 23360个
  2016-08-23 | Susan Calladine | 0 | 0

任何帮助将非常感谢,提前感谢

2 个答案:

答案 0 :(得分:0)

我希望这就是你想要的。如果没有让我知道

CREATE TABLE  ##Holidaystable
([Date] DATE, SusanCalladine INT, ClaireWillis INT, FazailaPirbhai INT);

INSERT INTO ##Holidaystable VALUES
('2016-08-22',  0, 0, 1),
('2016-08-23',  0, 1, 1),
('2016-08-24',  0, 1, 1),
('2016-08-25', 0, 1, 1),
('2016-08-26',  0, 0, 1)



CREATE TABLE  ##TelephonyTable
([CallDate] DATE, AgentName VARCHAR(50), InboundCallsAnswered INT, InboundHandleTime INT);
INSERT INTO ##TelephonyTable VALUES
('2016-08-22' , 'Claire Willis' , 0 , 0),
('2016-08-22' , 'Fazaila Pirbhai' , 36 , 17484),
('2016-08-22' , 'Susan Calladine' , 0 , 0),
('2016-08-23' , 'Claire Willis' , 12 , 4250),
('2016-08-23' , 'Fazaila Pirbhai' , 16 , 23360),
('2016-08-23' , 'Susan Calladine' , 0 , 0)

----Use a CTE for the query as below
;WITH Holiday AS
(SELECT 
H.[Date],
T.CallDate 
--,T.*
FROM ##Holidaystable H
INNER JOIN  ##TelephonyTable T
ON H.[Date]=T.CallDate
)
SELECT * FROM Holiday
PIVOT
(
COUNT([Date]) FOR CallDate IN ([2016-08-22],[2016-08-23])
) AS PV1

答案 1 :(得分:0)

您可以使用UNPIVOT从Holiday表中的列创建行,并通过DateAgentName列将结果与Telephony结合。

-- CREATE Holiday Table
CREATE TABLE Holiday
    (
      Date DATE NOT NULL ,
      [Susan Calladine] INT NULL ,
      [Claire Willis] INT NULL ,
      [Fazaila Pirbhai] INT NULL
    );

-- CREATE Telephony Table
CREATE TABLE Telephony
    (
      CallDate DATE NOT NULL ,
      AgentName NVARCHAR(100) NOT NULL ,
      InboundCallsAnswered INT NULL ,
      InboundHandleTime INT NULL
    );

-- Insert rows in Holiday
INSERT  INTO Holiday
VALUES  ( '2016-08-22', 0, 0, 1 ),
        ( '2016-08-23', 0, 1, 1 ),
        ( '2016-08-24', 0, 1, 1 ),
        ( '2016-08-25', 0, 1, 1 ),
        ( '2016-08-26', 0, 0, 1 );

-- Insert rows in Telephony
INSERT  INTO Telephony
VALUES  ( '2016-08-22', 'Claire Willis', 0, 0 ),
        ( '2016-08-22', 'Fazaila Pirbhai', 36, 17484 ),
        ( '2016-08-22', 'Susan Calladine', 0, 0 ),
        ( '2016-08-23', 'Claire Willis', 12, 4250 ),
        ( '2016-08-23', 'Fazaila Pirbhai', 16, 23360 ),
        ( '2016-08-23', 'Susan Calladine', 0, 0 );


-- Use UNPIVOT
WITH    Hol
          AS ( SELECT   Date ,
                        AgentName ,
                        Calls
               FROM     ( SELECT    *
                          FROM      Holiday
                        ) p UNPIVOT ( Calls FOR AgentName IN ( [Susan Calladine],
                                                              [Claire Willis],
                                                              [Fazaila Pirbhai] ) ) AS unpvt
             )
    SELECT  Date ,
            Hol.AgentName ,
            Calls ,
            InboundCallsAnswered ,
            InboundHandleTime
    FROM    Hol
            LEFT JOIN Telephony t ON Hol.Date = t.CallDate
                                     AND Hol.AgentName = t.AgentName
    ORDER BY Date ,
            AgentName;

结果将是:

enter image description here