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.
| Author |
Topic |
|
mickster
Starting Member
10 Posts |
Posted - 2010-11-09 : 19:53:13
|
| Hi,I'm hoping someone can help me with this. I have the following table (it's a snapshot, not the entire table which is 91,137 rows long). Apologies for the way the table appears below.Family Name| Subject Name----------- --------------ColeBrook | Ancient HistoryColeBrook | BiologyColeBrook | English AdvancedCooper | LRC CCooper | Italian ContinuersCooper | Italian ExtensionCooper | Advanced MathematicsCooper | Mathematics Extension 2I need to transpose the Subject Name column to rows, so each subject name would appear as Subject 1, Subject 2, Subject 3, etc.I'm trying to do this by using an additional 'Counter' (called SubjectNumber) column, I'm populating this column of the database by using the below statement.DECLARE @Counter INTSET @Counter = 0UPDATE MSET @Counter = SubjectNumber = @Counter + 1FROM Misc1 MINNER JOIN (SELECT StudentCode FROM Misc1 GROUP BY StudentCode) T ON M.StudentCode = T.StudentCodeThis statement obviously doesn't take into account the different groups of family names and instead generates a sequence of numbers from 1 to 91,137.Basically I want to have a sequence starting from 1 and ending at x (with x being the last row for that particular family name). Using the table above as an example, the Subject Number column would need to start at 1 for Colebrook and end at 3. Similarly, it would need to start at 1 for Cooper and end at 5. Note that the table is only a handful of rows. The actual table in my database is 91,137 rows long.Once I do this I will be able to transpose the column to rows fairly easily.Any thoughts? |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-10 : 00:04:06
|
| Can you please post the expected o/p ?PBUH |
 |
|
|
Sachin.Nand
2937 Posts |
|
|
mickster
Starting Member
10 Posts |
Posted - 2010-11-10 : 18:28:56
|
| Ok, I fixed the problem with the sequence using the following scriptDECLARE @iSeq int, @iLastVal nvarchar(255)SET @iSeq = 0UPDATE M SET M.SubjectNumber = @iSeq, @iSeq = CASE WHEN @iLastVal = M.FamilyName THEN @iSeq + 1 ELSE 1 END, @iLastVal = M.FamilyName FROM Misc MThis populates the sequence correctly.However I now have another problem with trying to transpose the rows into columns. Below are the first few rows of my table.FamilyName| SubjectNumber| SubjectName---------- ------------- -----------ColeBrook | 1| Ancient History ColeBrook | 2| BiologyColeBrook | 3| English AdvancedCooper | 1| LRC CCooper | 2| Italian ContinuersCooper | 3| Italian ExtensionCooper | 4| Advanced MathematicsI'd like to have my table appear as belowFamilyName| Subject1| Subject2| Subject3| Subject4---------- -------- --------- --------- --------ColeBrook| Ancient History| Biology|English AdvancedCooper|LRC C|Italian Continuers|Italian Extension|Advanced MathematicsI've tried using the following script to accomplish this but the results are not what I want as there are still multiple rows for each student and the Subject Names are not all on the same row.SELECTFamilyName,CASE SubjectNumber WHEN 1 THEN SubjectName ELSE '' END as Subject1,CASE SubjectNumber WHEN 2 THEN SubjectName ELSE '' END as Subject2,CASE SubjectNumber WHEN 3 THEN SubjectName ELSE '' END as Subject3,CASE SubjectNumber WHEN 4 THEN SubjectName ELSE '' END as Subject4FROM Misc MGROUP BY FamilyName, SubjectNumber, SubjectNameAppreciate any help, thanks |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-10 : 23:31:27
|
| [code]SELECTFamilyName,CASE SubjectNumber WHEN 1 THEN SubjectName ELSE '' END as Subject1,MAX(CASE SubjectNumber WHEN 2 THEN SubjectName ELSE '' END) as Subject2,MAX(CASE SubjectNumber WHEN 3 THEN SubjectName ELSE '' END) as Subject3,MAX(CASE SubjectNumber WHEN 4 THEN SubjectName ELSE '' END) as Subject4FROM Misc MGROUP BY FamilyName[/code]PBUH |
 |
|
|
|
|
|
|
|