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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Aged Yak Warrior

623 Posts

Posted - 03/25/2013 :  19:33:57  Show Profile  Reply with Quote

I need a query to get the expected output


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.

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 03/25/2013 :  20:41:33  Show Profile  Reply with Quote
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

Aged Yak Warrior

623 Posts

Posted - 03/25/2013 :  21:07:40  Show Profile  Reply with Quote
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

Flowing Fount of Yak Knowledge

2242 Posts

Posted - 03/26/2013 :  01:24:17  Show Profile  Reply with Quote
Try this...
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000