Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


PIVOT function


PIVOT function

Author
Message
muratistanbul
muratistanbul
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 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
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7001 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-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45223 Visits: 39927
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10342 Visits: 11350
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
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 102
Thanks for your help
muratistanbul
muratistanbul
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 102
Thanks for your help
vilonel
vilonel
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 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.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2823
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
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 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-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45223 Visits: 39927
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

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