Solved

SQL Pivot Error

Posted on 2014-01-23
3
364 Views
Last Modified: 2014-01-23
Hi

I am trying a basic SQL pivot example but am getting the error in the following statement:
Incorrect syntax near ')'.


Select * From [Pivot1] as S Pivot (Sum(Amount) For [Month]) As P
0
Comment
Question by:murbro
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39802398
you are missing the IN() part  ,  after [Month] part, and also not use *  for the select ...

see examples on the technet page here:
http://technet.microsoft.com/library/ms177410%28v=sql.105%29.aspx
0
 
LVL 12

Assisted Solution

by:Saurabh Bhadauria
Saurabh Bhadauria earned 250 total points
ID: 39802399
Follow below example to under stand Pivot functionality

DECLARE @pivot TABLE (
	amount INT
	,month CHAR(5)
	)

INSERT INTO @pivot (
	amount
	,month
	)
VALUES (2	,'JAN'	)	,
       (3	,'JAN'	) ,
       (4	,'DEC'	)

SELECT *
FROM @pivot AS S
Pivot(Sum(Amount) FOR [Month] IN (
			[JAN]
			,[DEC]
			)) AS PEND

Open in new window


Thanks,
Saurabh
0
 

Author Closing Comment

by:murbro
ID: 39802420
Thanks very much
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL LINE CONTINUATION ISSUE 12 32
Add different cell to otherwise similiar row 4 38
Sql server insert 13 27
Run SQL Server Proc from Access 11 28
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question