PIVOT function

  • Hi, When I use PIVOT function , ALL THE TIMES I GET

    Msg 170, Level 15, State 1, Line 7

    Line 7: Incorrect syntax near 'PIVOT'.

    Message is anybody have suggestion for me.

    Thanks in advance

  • muratistanbul (3/14/2010)


    Hi, When I use PIVOT function , ALL THE TIMES I GET

    Msg 170, Level 15, State 1, Line 7

    Line 7: Incorrect syntax near 'PIVOT'.

    Message is anybody have suggestion for me.

    Thanks in advance

    It's exactly what it says: The syntax you're using is wrong.

    If you'd actually post the query you currently have we might be able to help you with a more precise answer than "correct the syntax".



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Are you using SQL Server 2005 or better? If not, you'll need to use a cross-tab, instead.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Simple demo:

    DECLARE @demo

    TABLE (

    group_id INTEGER NOT NULL,

    value MONEY NOT NULL

    );

    INSERT @demo (group_id, value) VALUES (1, $1.00);

    INSERT @demo (group_id, value) VALUES (1, $2.00);

    INSERT @demo (group_id, value) VALUES (1, $3.00);

    INSERT @demo (group_id, value) VALUES (1, $4.00);

    INSERT @demo (group_id, value) VALUES (2, $5.00);

    INSERT @demo (group_id, value) VALUES (2, $4.00);

    INSERT @demo (group_id, value) VALUES (3, $3.00);

    INSERT @demo (group_id, value) VALUES (3, $2.00);

    INSERT @demo (group_id, value) VALUES (3, $7.00);

    INSERT @demo (group_id, value) VALUES (4, $2.00);

    SELECT group_id,

    value

    FROM @demo;

    SELECT P.[1], P.[2], P.[3], P.[4]

    FROM @demo D

    PIVOT (

    SUM(D.value) FOR

    D.group_id IN ([1], [2], [3], [4])

    ) P;

  • Thanks for your help

  • Thanks for your help

  • Hi,

    Newbie here - I am trying to run the simple pivot demo as on one of the above post (copy and paste) and I am getting the error:

    Msg 170, Level 15, State 1, Line 25

    Line 25: Incorrect syntax near 'PIVOT'.

    on SQL 2008

    Please help - thanks

  • Hi,

    Seems to work ok for me.

    Try removing whitespace after pasting the code, occasionally I've found odd (non-display) characters in code pasted from the forums.

  • I have removed the "big spaces" and PF5 the sql qry but still get the Incorrect syntax near 'PIVOT' error - do you think it could be my SQL thats messing around or am I missing something in the below sql code - thanks

    DECLARE @demo

    TABLE (

    group_id INTEGER NOT NULL,

    value MONEY NOT NULL

    );

    INSERT @demo (group_id, value) VALUES (1, $1.00);

    INSERT @demo (group_id, value) VALUES (1, $2.00);

    INSERT @demo (group_id, value) VALUES (1, $3.00);

    INSERT @demo (group_id, value) VALUES (1, $4.00);

    INSERT @demo (group_id, value) VALUES (2, $5.00);

    INSERT @demo (group_id, value) VALUES (2, $4.00);

    INSERT @demo (group_id, value) VALUES (3, $3.00);

    INSERT @demo (group_id, value) VALUES (3, $2.00);

    INSERT @demo (group_id, value) VALUES (3, $7.00);

    INSERT @demo (group_id, value) VALUES (4, $2.00);

    SELECT group_id,

    value

    FROM @demo;

    SELECT P.[1], P.[2], P.[3], P.[4]

    FROM @demo D PIVOT(SUM(D.value) FOR D.group_id IN ([1], [2], [3], [4])) P;

  • vilonel (4/8/2011)


    I have removed the "big spaces" and PF5 the sql qry but still get the Incorrect syntax near 'PIVOT' error - do you think it could be my SQL thats messing around or am I missing something in the below sql code - thanks

    DECLARE @demo

    TABLE (

    group_id INTEGER NOT NULL,

    value MONEY NOT NULL

    );

    INSERT @demo (group_id, value) VALUES (1, $1.00);

    INSERT @demo (group_id, value) VALUES (1, $2.00);

    INSERT @demo (group_id, value) VALUES (1, $3.00);

    INSERT @demo (group_id, value) VALUES (1, $4.00);

    INSERT @demo (group_id, value) VALUES (2, $5.00);

    INSERT @demo (group_id, value) VALUES (2, $4.00);

    INSERT @demo (group_id, value) VALUES (3, $3.00);

    INSERT @demo (group_id, value) VALUES (3, $2.00);

    INSERT @demo (group_id, value) VALUES (3, $7.00);

    INSERT @demo (group_id, value) VALUES (4, $2.00);

    SELECT group_id,

    value

    FROM @demo;

    SELECT P.[1], P.[2], P.[3], P.[4]

    FROM @demo D PIVOT(SUM(D.value) FOR D.group_id IN ([1], [2], [3], [4])) P;

    Which version of SQL Server are you using?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (4/8/2011)


    Which version of SQL Server are you using?

    Ah-ha! Good point Jeff. But he did mention 2008 in an earlier post.

  • Using SQL 2008 - copied this info from SQL "about" form:

    Microsoft SQL Server Management Studio 10.0.4000.0

    Microsoft Analysis Services Client Tools 10.0.4000.0

    Microsoft Data Access Components (MDAC) 6.1.7600.16385

    Microsoft MSXML 3.0 4.0 5.0 6.0

    Microsoft Internet Explorer 8.0.7600.16385

    Microsoft .NET Framework 2.0.50727.4952

    Operating System 6.1.7600

    Is the problem with my SQL install - must I re-install my SQL 2008?

    Thanks for the assistance

  • That only tells you the versions of the client tools etc installed on your machine, not the version of the database engine.

    Run this in a query window:

    SELECT @@VERSION

  • nigel. (4/11/2011)


    That only tells you the versions of the client tools etc installed on your machine, not the version of the database engine.

    Run this in a query window:

    SELECT @@VERSION

    will do and post asap

  • vilonel (4/11/2011)


    nigel. (4/11/2011)


    That only tells you the versions of the client tools etc installed on your machine, not the version of the database engine.

    Run this in a query window:

    SELECT @@VERSION

    will do and post asap

    While you're there, check the compatibility mode setting.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic. Login to reply