Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I want to select one column from a set of columns in a table but the columns will not be the same for each record. See below for an example as I'm having a really hard time explaining this...In the example below I would like to select the last 'Month' column that isn't NULL for each record in the table.So for ID 01 I want to select only Month4 and for ID 02 I want to select only Month3.
I'm trying to do this without building temp tables as I'm using this inside of an application that is limited but at this point I'll take any method possible.
quote:Originally posted by Lamprey Try the COALESCE funtion:
SELECT ID, Products, COALESCE(Month4, Month3, Month2, Month1) AS NotNullMonthValueFROM @T
Worked perfectly. Thanks so much.
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts
Posted - 2010-09-10 : 13:14:38
I would think that normalizing your data would be the BEST solution http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp
aiccucs
Starting Member
7 Posts
Posted - 2010-09-10 : 16:08:56
quote:Originally posted by DonAtWork I would think that normalizing your data would be the BEST solution
I agree, but unfortunately I do not have the access to do that...