| Author |
Topic |
|
Sarakumar
Posting Yak Master
108 Posts |
Posted - 2006-07-19 : 04:57:32
|
| DECLARE @STUDENT TABLE(SID INT,SNAME VARCHAR(20),MARK1 INT,MARK2 INT,MARK3 INT,MARK4 INT,MARK5 INT)INSERT INTO @STUDENT select 1,'SONE',80,70,50,60,70 UNION allselect 2,'STWO',60,65,85,76,80 UNION allselect 3,'Sthree',60,65,85,76,80 my Table Structure is like the above. but i want to select in the rowwise, means, Sone stwo SthreeMark1 Mark2 Mark3Mark4 Mark5 Can we get this in the query it self, or do i need to do it in the front end. Advise pls |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-07-19 : 05:55:56
|
It is not clear what you want. Try one of these-- This vector?select z.valuefrom ( select 10 * sid grp, sname value from @student union all select 10 * sid +1, convert(varchar, mark1) from @student union all select 10 * sid +2, convert(varchar, mark2) from @student union all select 10 * sid +3, convert(varchar, mark3) from @student union all select 10 * sid +4, convert(varchar, mark4) from @student union all select 10 * sid +5, convert(varchar, mark5) from @student ) zorder by z.grp-- Or this matrix?SELECT MAX(CASE WHEN sid = 1 THEN mark ELSE 0 END) 'Sone', MAX(CASE WHEN sid = 2 THEN mark ELSE 0 END) 'Stwo', MAX(CASE WHEN sid = 3 THEN mark ELSE 0 END) 'Sthree'FROM ( SELECT sid, 1 Lvl, mark1 mark FROM @Student UNION ALL SELECT sid, 2, mark2 FROM @Student UNION ALL SELECT sid, 3, mark3 FROM @Student UNION ALL SELECT sid, 4, mark4 FROM @Student UNION ALL SELECT sid, 5, mark5 FROM @Student ) zGROUP BY z.Lvl Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|