tags:

views:

47

answers:

4

I have a table strcuture (golf score card) as follow:

HoleID int
CourseID int
Par INT
Distance INT
LowIndex INT
HighIndex INT
Sequence INT (representing the sequence of the hole: 1, 2, 3, ... 18)

This course has 18 holes.

I want to pivoting into:

Hole,      1, 2, 3, ... 18
Par,       X, X, X, ... X
Distance,  Y, Y, Y, ... Y
LowIndex,  Z, Z, Z, ... Z
HighIndex, A, A, A, ... A

I can achieve this by using cursor but is there any better way?

+1  A: 

TSQL has direct support for pivoting. Take a look here and here, for example.

Ronald Wildenberg
SQL2000 doesn't :(
John Nolan
where does the poster say they are using SQL Server 2000?
Mitch Wheat
I am using SQL 2005
dewacorp.alliances
+1  A: 

You could do

SELECT HoleID FROM Card WHERE  HoleId =1 ,(SELECT  HoleID   FROM Card WHERE  HoleId =2) , -- etc  

UNION
SELECT Par FROM Card WHERE HoleId = 1, (SELECT Par FROM Card where HoleID=2). --etc
UNION
SELECT Distance FROM Card WHERE HoleId = 1, (SELECT Distance FROM Card where HoleID=2). --etc
UNION
SELECT LowIndex FROM Card WHERE HoleId = 1, (SELECT LowIndex FROM Card where HoleID=2). --etc
UNION
SELECT HIghIndex FROM Card WHERE HoleId = 1, (SELECT HighIndex FROM Card where HoleID=2). --etc

if you were desperate to get rid of the cursor.

Make sure that you make all the datatypes the same through a cast or something.

John Nolan
A: 

Got it the first part only ... that will do.

select [1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18]
from (Select Sequence, Par FROM holes WHERE CourseID=1) a

PIVOT 
(
    Max(Par)
    FOR [Sequence] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18])

) AS p
dewacorp.alliances
A: 

Complete list:

select 'Par', [1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18]
from (Select Sequence, Par FROM holes WHERE CourseID=1) a

PIVOT 
(
    Max(Par)
    FOR [Sequence] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18])

) AS p

UNION

select 'Distance', [1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18]
from (Select Sequence, Distance FROM holes WHERE CourseID=1) a

PIVOT 
(
    Max(Distance)
    FOR [Sequence] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18])

) AS p

UNION


select 'LowIndex', [1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18]
from (Select Sequence, LowIndex FROM holes WHERE CourseID=1) a

PIVOT 
(
    Max(LowIndex)
    FOR [Sequence] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18])

) AS p

UNION

select 'HighIndex', [1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18]
from (Select Sequence, HighIndex FROM holes WHERE CourseID=1) a

PIVOT 
(
    Max(HighIndex)
    FOR [Sequence] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9], [10], [11], [12], [13], [14], [15], [16], [17], [18])

) AS p
dewacorp.alliances