TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jes Sie
741
1.2k
281.9k
Pivot in SQL Server 2014
Jun 6 2017 3:23 AM
Hello, I need to do a pivot in sql server 2014 but failed. I read several forums but none of them helped. Below is my Table that I would like to pivot:
Below is my desired result:
This is what I have so far but have an error:
DECLARE
@Query
AS
NVARCHAR(
MAX
)
DECLARE
@PivotProductType
AS
NVARCHAR(
MAX
)
--Get unique values of pivot column
SELECT
@PivotProductType=
COALESCE
(@PivotProductType +
','
,
''
) + QUOTENAME(ProductType)
FROM
(
SELECT
DISTINCT
ProductType
FROM
tblAgentCommission)
AS
PivotColumn
SELECT
@PivotProductType
--Create the dynamic query with all the values for
--pivot column at runtime
SET
@Query =
N
'SELECT Country, '
+ @PivotProductType + '
FROM
[dbo].[PivotColumn]
PIVOT(CommissionRate)
FOR
ProductType
IN
(
' + @PivotProductType + '
))
AS
P'
SELECT
@Query
--Execute dynamic query
EXEC
sp_executesql @Query
(1 row(s) affected)
(1 row(s) affected)
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ')'.
Thanks in Advance.
Reply
Answers (
7
)
delete primary and foreign key from the form side
Code breaks from the loop in C# windows form