A Brun
A Brun

Reputation: 1

PIVOt Function in SQL Server

I am getting an error on the Syntax for my PIVOT function, an am unable to find out why.

My query is:

   SELECT Month_ID
    ,Key
    ,C.App_ID
    ,APP_Name
    FROM Usage C
    INNER JOIN Applications B ON C.App_ID = B.App_ID
    WHERE C.Key IN ('6853',
    '6859',
    '6861',
    '6863',
    '8580',
    '8626',
    '8639',
    '8717',
    '8721',
    '8731',
    '8737',
    '8744',
    '8754',
    '8873',
    '8876',
    '8884',
    '9014',
    '9080',
    '9083',
    '9085',
    '9097',
    '9101',
    '9115',
    '9117',
    '9353',
    '9354',
    '9358',
    '9380',
    '9383',
    '9385',
    '9411',
    '9471',
    '9514',
    '9587',
    '9594',
    '9595',
    '9598')
PIVOT(MAX(B.App_ID) FOR App_NAME IN (
"Data Management Services",
"intelliSource",
"Price Performance",
"Marketplace_Procure",
"Spend Essentials",
"Apt",
"Prov")) AS PVT

And the error I keep getting is:

Msg 156, Level 15, State 1, Line 38053 Incorrect syntax near the keyword 'PIVOT'.

Upvotes: 0

Views: 66

Answers (1)

paulbarbin
paulbarbin

Reputation: 382

Edited, since I don't have this structure, I can't test it on my side. If you include the table structures and data, it would make for less work for the person answering (for future reference).

    SELECT
    * 
FROM

(SELECT 
    Month_ID ,Key ,C.App_ID ,APP_Name

FROM 
    Usage C 
    INNER JOIN Applications B ON C.App_ID = B.App_ID 
WHERE 
    C.Key IN ('6853', '6859', '6861', '6863', '8580', '8626', '8639', '8717', '8721', '8731', '8737', '8744', '8754', '8873', '8876', '8884', '9014', '9080', '9083', '9085', '9097', '9101', '9115', '9117', '9353', '9354', '9358', '9380', '9383', '9385', '9411', '9471', '9514', '9587', '9594', '9595', '9598')
) as source
PIVOT(MAX(B.App_ID) FOR App_NAME IN ( "Data Management Services", "intelliSource", "Price Performance", "Marketplace_Procure", "Spend Essentials", "Apt", "Prov")) AS PVT

Upvotes: 2

Related Questions