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 2008 Forums
 Transact-SQL (2008)
 Query

Author  Topic 

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2013-03-25 : 19:33:57
[code]

I need a query to get the expected output

Table:Student

stguid stuName studwor stid
------------------------ ----------------- ----------------- -----------
642-4d5d-9af0-4c7a18dd ChrisName Chris 255
4171-8655-2de255b88e08 ChrisCity SAN City 179
2a0d-4100-bd1c-343882 ChrisCounty Wendy 179
48f0-b455-5207b187e639 ChrisphoneNumber This is a test phone 179
4d5d-9af0-4c7a18ddd7b2 ChrisDName WTS Test 180
4041-ba50-1085acf7d86c ChrisDType This is for Dtpetest 180


Expected output:

ChrisName ChrisCity ChrisCounty ChrisphoneNumber ChrisphoneNumber ChrisDType
---------- ------------ ------------- ----------------- ---------------- ----------
Chris SAN City Wendy This is a test phone WTS Test This is for Dtpetest

Thanks 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?
Go to Top of Page

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..
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-26 : 01:24:17
Try this...
SELECT *
FROM (SELECT stuName, studwor FROM Student)p
PIVOT (MAX(studwor) FOR stuName IN ([ChrisName],[ChrisCity],[ChrisCounty],[ChrisphoneNumber],[ChrisDName], [ChrisDType]))pvt

Go to Top of Page
   

- Advertisement -