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 2005 Forums
 Transact-SQL (2005)
 select query issue

Author  Topic 

Looper
Yak Posting Veteran

68 Posts

Posted - 2011-06-09 : 04:59:31
I have a table (table PS) with 20 fields e.g. currentvalue1, currentvalue2 ...etc These fields are decimal(8,2) Null. I want to get the max value i.e. the last currentvalue with a non 0 value/null held. I want this added to my current select statement

e.g. select name, age, sex ....etc where id = @id

Any ideas

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-09 : 07:08:31
Not quite clear to me what you mean by "max value", but one of these?

select name, age, sex, ...,
coalesce(currentvalue20,currentvalue19,currentvalue18,...,currentvalue1) from ....

or

select name, age, sex, ...,
coalesce(nullif(currentvalue20,0),nullif(currentvalue19,0),nullif(currentvalue18,0),...,nullif(currentvalue1,0)) from ....

Go to Top of Page

Looper
Yak Posting Veteran

68 Posts

Posted - 2011-06-09 : 07:30:53
that has solved it

thanks
Go to Top of Page
   

- Advertisement -