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 |
sqlfresher2k7
Aged Yak Warrior
623 Posts |
Posted - 2013-03-25 : 19:33:57
|
[code]I need a query to get the expected outputTable:Studentstguid stuName studwor stid------------------------ ----------------- ----------------- -----------642-4d5d-9af0-4c7a18dd ChrisName Chris 2554171-8655-2de255b88e08 ChrisCity SAN City 1792a0d-4100-bd1c-343882 ChrisCounty Wendy 17948f0-b455-5207b187e639 ChrisphoneNumber This is a test phone 1794d5d-9af0-4c7a18ddd7b2 ChrisDName WTS Test 1804041-ba50-1085acf7d86c ChrisDType This is for Dtpetest 180Expected output:ChrisName ChrisCity ChrisCounty ChrisphoneNumber ChrisphoneNumber ChrisDType---------- ------------ ------------- ----------------- ---------------- ---------- Chris SAN City Wendy This is a test phone WTS Test This is for DtpetestThanks for help in advance.[/code] |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-03-25 : 20:41:33
|
You can use the PIVOT operator to get the result you are looking for, except for one thing - you need a column that identifies all the rows associated with a student. I would have thought that that identifier would be stid, but in your example there are 3 student id's 255, 179, and 180. So that cannot be it. Is there another column that allows you to group the rows for a given student together? |
|
|
sqlfresher2k7
Aged Yak Warrior
623 Posts |
Posted - 2013-03-25 : 21:07:40
|
Thanks for the response but I don't have other column to group the students..Can you please the query.. |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-26 : 01:24:17
|
Try this...SELECT *FROM (SELECT stuName, studwor FROM Student)pPIVOT (MAX(studwor) FOR stuName IN ([ChrisName],[ChrisCity],[ChrisCounty],[ChrisphoneNumber],[ChrisDName], [ChrisDType]))pvt |
|
|
|
|
|