SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

sqlfresher2k7
Aged Yak Warrior

609 Posts

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


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.

James K
Flowing Fount of Yak Knowledge

3575 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

sqlfresher2k7
Aged Yak Warrior

609 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

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 03/26/2013 :  01:24:17  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000