使用MS SQL Server中的“分组依据”字段将垂直sql结果转换为水平输出

时间:2014-04-15 03:56:34

标签: sql sql-server sql-server-2008 pivot

我有一个表(在SQL Server 2008中),它有垂直销售交易数据,我想将垂直输出转换为水平输出,我试图使用PIVOT,但有些人怎么不知道如何在PIVOT上应用group by ,因为我想要基于AccountHeadID,TransType和IsPast Column的Sum。

样本表

CREATE TABLE Trans
    ([TransID] int,
     [CustID] int,
     [AccountHeadID] int,
     [TransType] varchar(100),
     [IsPast] bit,
     [Amount] int)
;

INSERT INTO Trans
    ([TransID],CustID, [AccountHeadID], [TransType], [IsPast],[Amount])
VALUES
    (1, 1, 1, 'Sales',1,1000),
    (2, 1, 1, 'Sales',0,500),
    (3, 1, 1, 'Sales',0,400),
    (4, 1, 1, 'Return',0,300),
    (5, 1, 1, 'Return',0,200),
    (6, 1, 1, 'Return',0,100),
    (7, 1, 1, 'Return',1,150),
    (8, 1, 2, 'Sales',1,2000)   

当前查询

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(AccountHeadID)  
                    from Trans
                    group by AccountHeadID
                    order by AccountHeadID
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query = 'SELECT CustID,' + @cols + ' 
            from 
            (
              SELECT 
                a.CustID,
                a.AccountHeadID,
                 a.TransType,
                 a.Amount,
                 a.isPast
              FROM Trans a
            ) x
            pivot 
            (
                sum(Amount)
                for AccountHeadID in (' + @cols + ')
            ) p '

execute sp_executesql @query;

预期输出

CustID | Account-HeadID-TransType-IsPast[1-Sales-Past] |  Account-HeadID-TransType-IsCurrent[1-Sales-Current] | Account-HeadID-TransType-IsCurrent[1-Return-Past] |  Account-HeadID-TransType-IsCurrent[1-Return-Current] | Account-HeadID-TransType-IsCurrent[2-Sales-Past]| ...
1      |1000                                           | 900 (500 + 400)                                      |150                                                | 600[300+200+100]                                       |2000   

请参阅SQL Fiddle with Demo

欢迎任何建议或意见!

由于 苏雷什

1 个答案:

答案 0 :(得分:1)

试试这个:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(NVARCHAR(10),AccountHeadID) + N'-' + TransType + N'-' + CONVERT(NVARCHAR(10),IsPast))  
                    from Trans
                    group by AccountHeadID, TransType, IsPast
                    order by AccountHeadID, TransType, IsPast
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

--select @cols

set @query = 'SELECT CustID,' + @cols + ' 
            from 
            (
              SELECT 
                a.CustID,
                CONVERT(NVARCHAR(10),AccountHeadID) + N''-'' + TransType + N''-'' + CONVERT(NVARCHAR(10),IsPast) Acct,
                 a.Amount
              FROM Trans a
            ) x
            pivot 
            (
                sum(Amount)
                for Acct in (' + @cols + ')
            ) p '

execute sp_executesql @query;

SQL Fiddle