Swap Some Rows into Columns in SQL Server 2008

2019-09-08 11:33发布

问题:

Another one for you SQL Server gurus...

I have a table like so...

AccountManager | Action                | Count
-----------------------------------------------------
Joe            | Client Negotiation    | 10
Bloggs         | Closing               | 1
Aunty          | Email                 | 12
Marie          | Preparing Contract    | 32

The Action column may have numerous different types of statuses which may not necessarily be fixed.

I need the output to be the following:

AccountManager | Client Negotiation | Closing | Email | Preparing Contract
--------------------------------------------------------------------------
Joe            | 10                 | 0       | 0     | 0
Bloggs         | 0                  | 1       | 0     | 0
Aunty          | 0                  | 0       | 12    | 0
Marie          | 0                  | 0       | 0     | 32

How could this be achieved? Help!

回答1:

Follow the below walkthrough

http://www.tsqltutorials.com/pivot.php



回答2:

Maybe something like this:

First the test data:

CREATE TABLE #tbl (AccountManager VARCHAR(100), Action VARCHAR(100),Count INT)

INSERT INTO #tbl
VALUES
    ('Joe','Client Negotiation',10),
    ('Bloggs','Closing',1),
    ('Aunty','Email',12),
    ('Marie','Preparing Contract',32)

If you know that the columns are static. Then you can do this:

SELECT
    AccountManager,
    ISNULL([Client Negotiation],0) AS [Client Negotiation],
    ISNULL([Closing],0) AS [Closing],
    ISNULL([Email],0) AS [Email],
    ISNULL([Preparing Contract],0) AS [Preparing Contract]
FROM
(
SELECT
    tbl.AccountManager,
    tbl.Action,
    tbl.Count
FROM
    #tbl AS tbl
) AS p
PIVOT
(
    SUM([Count])
    FOR [Action] IN([Client Negotiation],[Closing],[Email],[Preparing Contract])
) AS pvt

Otherwise you have to do a dynamic pivot like this:

First the unique column names:

DECLARE @cols VARCHAR(MAX),
        @colsWithIsNull VARCHAR(MAX)
;WITH CTE
AS
(
    SELECT
        ROW_Number() OVER(PARTITION BY tbl.Action ORDER BY tbl.Action) AS iRank,
        tbl.Action
    FROM
        #tbl AS tbl
)
SELECT  @cols = COALESCE(@cols + ','+QUOTENAME(Action),
                     QUOTENAME(Action)),
        @colsWithIsNull=COALESCE(@colsWithIsNull + ',ISNULL('+QUOTENAME(Action)+',0) AS '+QUOTENAME(Action),
                     'ISNULL('+QUOTENAME(Action)+',0) AS '+QUOTENAME(Action))
FROM
    CTE
WHERE
    iRank=1

Then the dynamic pivot like this:

DECLARE @query NVARCHAR(4000)=
N'SELECT
    AccountManager,
    '+@colsWithIsNull+'
FROM
(
SELECT
    tbl.AccountManager,
    tbl.Action,
    tbl.Count
FROM
    #tbl AS tbl
) AS p
PIVOT
(
    SUM([Count])
    FOR [Action] IN('+@cols+')
) AS pvt'

EXECUTE(@query)

Then in my case I will drop the temp table:

DROP TABLE #tbl


回答3:

select [AccountManager], ISNULL([Email],0) as [Email], ISNULL([Closing],0) as [Closing], ISNULL([Preparing Contact],0) as [Preparing Contact], ISNULL([Client Negotiation],0) as [Client Negotiation] from  
    (SELECT * FROM TestPivot) as SourceTable

Pivot
(
    SUM([Count]) 
    For [Action] in ([Email], [Closing], [Preparing Contact], [Client Negotiation])
) as PivotedColumns
order by [Email] desc, [Closing] desc, [Preparing Contact] desc, [Client Negotiation] desc


回答4:

This is best handled by the reporting tool that you're using. Look for "Pivot table".