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)
 Selecting Fields as Columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tomrippity
Starting Member

USA
33 Posts

Posted - 08/15/2012 :  11:33:23  Show Profile  Reply with Quote
I am trying to select from a table where field names show up as columns, so that instead of getting 5 rows of data, each with 1 data type seperating them, I get 1 row with an additional 5 columns. I have the current result and desired result as an excel file linked below.

Thank you in advance!

http://dl.dropbox.com/u/55890623/result_set.xls

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 08/15/2012 :  11:49:58  Show Profile  Reply with Quote

SELECT DISTRICT_STUDENT_ID,	
         STATE_STATE_ID,	
         FIRST_NAME,
	LAST_NAME,
MAX(CASE WHEN SHORT_DESCRIPTION = 'WEIGHT' THEN SCORE END) AS WEIGHT,
MAX(CASE WHEN SHORT_DESCRIPTION = 'HEIGHT' THEN SCORE END) AS HEIGHT,
MAX(CASE WHEN SHORT_DESCRIPTION = 'CURL_UPS' THEN SCORE END) AS CURL_UPS,
MAX(CASE WHEN SHORT_DESCRIPTION = 'SIT_REACH' THEN SCORE END) AS SIT_REACH,
MAX(CASE WHEN SHORT_DESCRIPTION = 'MILE' THEN SCORE END) AS MILE,
DATE_ENTERED
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY DISTRICT_STUDENT_ID,	
         STATE_STATE_ID,	
         FIRST_NAME,
	LAST_NAME
ORDER BYDATE_ENTERED DESC) AS Rn,*
FROM table
)t
WHERE Rn=1
GROUP BY DISTRICT_STUDENT_ID,	
         STATE_STATE_ID,	
         FIRST_NAME,
	LAST_NAME,
DATE_ENTERED



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tomrippity
Starting Member

USA
33 Posts

Posted - 08/15/2012 :  12:04:50  Show Profile  Reply with Quote
Thank you Visakh16!

I am also curious, is there anyway to automate adding columns?

For example, a school might add new exercises, which would then require me to go in and modify this query to reflect the new columns. Is there someway I can fix it so if they add an exercise "push-ups" lets say, that it automatically adds the columns "PUSH_UPS"?

Essentially replace this:

MAX(CASE WHEN SHORT_DESCRIPTION = 'WEIGHT' THEN SCORE END) AS WEIGHT,
MAX(CASE WHEN SHORT_DESCRIPTION = 'HEIGHT' THEN SCORE END) AS HEIGHT,
MAX(CASE WHEN SHORT_DESCRIPTION = 'CURL_UPS' THEN SCORE END) AS CURL_UPS,
MAX(CASE WHEN SHORT_DESCRIPTION = 'SIT_REACH' THEN SCORE END) AS SIT_REACH,
MAX(CASE WHEN SHORT_DESCRIPTION = 'MILE' THEN SCORE END) AS MILE

with some query to build the columns

Thanks!

Edited by - tomrippity on 08/15/2012 12:08:30
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 08/15/2012 :  12:29:14  Show Profile  Reply with Quote
see

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.05 seconds. Powered By: Snitz Forums 2000