SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


PIVOT function


PIVOT function

Author
Message
muratistanbul
muratistanbul
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 102
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
LutzM
LutzM
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38119 Visits: 13559
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
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)

Group: General Forum Members
Points: 339087 Visits: 42614
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Paul White
Paul White
SSC Guru
SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)

Group: General Forum Members
Points: 54610 Visits: 11391
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;





Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
muratistanbul
muratistanbul
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 102
Thanks for your help
muratistanbul
muratistanbul
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 102
Thanks for your help
vilonel
vilonel
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 228
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
nigel.
nigel.
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4672 Visits: 2938
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.

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

vilonel
vilonel
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 228
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;

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)SSC Guru (339K reputation)

Group: General Forum Members
Points: 339087 Visits: 42614
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search