MSSQL - 透视多字段

时间:2016-02-23 12:37:35

标签: sql-server

我有下表&数据:

USE tempdb; SET NOCOUNT ON;
IF OBJECT_ID('tempdb.dbo.#abcd', 'U') IS NOT NULL DROP TABLE dbo.#abcd;
CREATE TABLE #abcd 
(
    [Company] [NVARCHAR](30) NOT NULL,
    [FatherNum] [NVARCHAR](30) NOT NULL,
    [FormatCode] [NVARCHAR](20) NULL,
    [AcctName] [NVARCHAR](8) NULL,
);

INSERT INTO [#abcd] ([Company],[FatherNum],[FormatCode],[AcctName])VALUES('FR','100002','123456','ACCOUNT1');
INSERT INTO [#abcd] ([Company],[FatherNum],[FormatCode],[AcctName])VALUES('FR','100002','123321','ACCOUNT2');
INSERT INTO [#abcd] ([Company],[FatherNum],[FormatCode],[AcctName])VALUES('UK','100002','123456','ACCOUNT1');
INSERT INTO [#abcd] ([Company],[FatherNum],[FormatCode],[AcctName])VALUES('UK','100002','123321','ACCOUNT3');
INSERT INTO [#abcd] ([Company],[FatherNum],[FormatCode],[AcctName])VALUES('UK','100003','125467','TESTA');
INSERT INTO [#abcd] ([Company],[FatherNum],[FormatCode],[AcctName])VALUES('USA','100000','45725','TESTB');
INSERT INTO [#abcd] ([Company],[FatherNum],[FormatCode],[AcctName])VALUES('USA','100002','123456','ACCOUNT1');

查询此表的所需结果是:

USE tempdb; SET NOCOUNT ON;
IF OBJECT_ID('tempdb.dbo.#abcd1', 'U') IS NOT NULL DROP TABLE dbo.#abcd1;
CREATE TABLE #abcd1 
(

    [FatherNum] [NVARCHAR](30) NOT NULL,
    [FR] [NVARCHAR](20) NULL,
    [FRName] [NVARCHAR](20) NULL,
    [UK] [NVARCHAR](20) NULL,
    [UKName] [NVARCHAR](20) NULL,
    [USA] [NVARCHAR](20) NULL,
    [USAName] [NVARCHAR](20) NULL,

);

INSERT INTO [#abcd1] ([FatherNum],[FR],[FRName], [UK], [UKName], [USA], [USAName]) VALUES('100002','123456','ACCOUNT1','123456','ACCOUNT1','123456','ACCOUNT1');
INSERT INTO [#abcd1] ([FatherNum],[FR],[FRName], [UK], [UKName], [USA], [USAName]) VALUES('100002','123321','ACCOUNT2','123321','ACCOUNT3',NULL,NULL);
INSERT INTO [#abcd1] ([FatherNum],[FR],[FRName], [UK], [UKName], [USA], [USAName]) VALUES('100003',NULL,NULL,'125467','TESTA',NULL,NULL);
INSERT INTO [#abcd1] ([FatherNum],[FR],[FRName], [UK], [UKName], [USA], [USAName]) VALUES('100003',NULL,NULL,NULL,NULL,'45725','TESTB');

SELECT * FROM dbo.#abcd1

我只能做到这一点:

SELECT * FROM (
SELECT Company, FatherNum, FormatCode,AcctName FROM #abcd 
) temptable
PIVOT
(MAX(FormatCode) FOR Company IN ([FR], [UK], [USA])) T1

1 个答案:

答案 0 :(得分:2)

在转动之前,您需要unpivot数据

SELECT FatherNum,
       [FR],
       [FR Name],
       [UK],
       [UK Name],
       [USA],
       [USA Name]
FROM   (SELECT FatherNum,
               FormatCode,
               NEW,
               value
        FROM   #abcd
               CROSS APPLY (VALUES (Company,FormatCode),
                                   (Company + ' NAME',AcctName)) CS (NEW, value)) temptable
       PIVOT (Max(value)
             FOR NEW IN ([FR],[FR Name],
                         [UK],[UK Name],
                         [USA],[USA Name])) T1 

<强>结果:

╔═══════════╦════════╦══════════╦════════╦══════════╦════════╦══════════╗
║ FatherNum ║   FR   ║ FR Name  ║   UK   ║ UK Name  ║  USA   ║ USA Name ║
╠═══════════╬════════╬══════════╬════════╬══════════╬════════╬══════════╣
║    100002 ║ 123321 ║ ACCOUNT2 ║ 123321 ║ ACCOUNT3 ║ NULL   ║ NULL     ║
║    100002 ║ 123456 ║ ACCOUNT1 ║ 123456 ║ ACCOUNT1 ║ 123456 ║ ACCOUNT1 ║
║    100003 ║ NULL   ║ NULL     ║ 125467 ║ TESTA    ║ NULL   ║ NULL     ║
║    100000 ║ NULL   ║ NULL     ║ NULL   ║ NULL     ║ 45725  ║ TESTB    ║
╚═══════════╩════════╩══════════╩════════╩══════════╩════════╩══════════╝