Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Cross table

Author  Topic 

Bertrandkis
Starting Member

8 Posts

Posted - 2006-03-20 : 03:47:17
How can I create a cross-table from a flat table. The inintial table and cross table are shown below.
Learner Course code Marks obtained
--------------------------------------------------------
Leticia Eksteen 9042 52
Leticia Eksteen 9122 48
Leticia Eksteen 9147 85
Victoria Collins 9042 70
Victoria Collins 9122 60
Victoria Collins 9147 80
John Best 9042 78
John Best 9122 55
John Best 9147 66
John Best 9991 100

The manager want to see the records in a cross-tab format like this:
Learner code_9042 code_9122 code_9147 Code_9991
L.Eksteen 52 48 85 NULL
V.Collins 70 60 80 NULL
J.Best 78 55 66 100

Le Bon vivant

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-03-20 : 03:55:20
just check out this link

http://www.sqlteam.com/searchresults.asp?SearchTerms=Crosstab

Sucess Comes to those who Believe in Beauty of their Dream..
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-03-20 : 05:20:17
Or if you use v2005
select Learner = left(Learner, 1) + '.' + right(Learner, charindex(' ', reverse(Learner))), code_9042 = [9042], code_9122 = [9122], code_9147 = [9147], code_9991 = [9991]
from
(
select Learner, Course, Marks
from #a
) a
PIVOT
(
sum(Marks) for Course in ([9042], [9122], [9147], [9991])
) pvt

(just wanted to see how easy it was)

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-20 : 06:49:38
Refer this
http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -