Hi
I have some trouble getting a max value off several columns for each row,
the sql funktion MAX (string) can I only get to work on at single column, so how do I do'it
if I want to get the MAX value in the X-Axis of a table.
Thanks in advance I hope somebody can help
/Kenneth Worm

Need to query max value between several colums for each row ?
FruitBatInShades
your table looks like
Table_name(
Id int,
col1 string,
col2 string,
col3 string,
col4 string,
col5 string,
col6 string,
col7 string
ETC,,,,)
and for each id you want the greatest string between (col1.....col7)
Create a new function
FUNCTION greatest (@string1 char,@string2 char )
RETURNS char
AS
BEGIN
declare @ret char
if @string1 > @string2
set @ret= @string1
else set @ret= @string2
return( @ret)
END
and then
select id,
dbo.greatest(col1,
dbo.greatest(col2,
dbo.greatest(col3,
dbo.greatest(col4,
dbo.greatest(col5,
dbo.greatest(col6,col7))))))
from table_name
...
Madhu.Babu
I got it too work with the 1'st guy's answer, but yours look intersting too, will there be a performance gain, when you put the logic into a funtion or will it be the same as the previous answer
Here's how I did it, My problem has just been that I can't work around with the query output I got,
So I did some work aound that and made a flat file output which I then have to import again into the table "data.MaxVaerdi" is there a smarter way so that I don't have to make 2 jobs pushing and pulling data from a flat txt file
/Kenneth
my code ex.
select dbo.Data_Storage.id_datasource,
MaxVaerdi = max(case c.n
when 1 then dbo.Data_Storage.Data00001
when 2 then dbo.Data_Storage.Data00002
when 3 then dbo.Data_Storage.Data00003
when 4 then dbo.Data_Storage.Data00004
when 5 then dbo.Data_Storage.Data00005
when 6 then dbo.Data_Storage.Data00006
end)
from dbo.Data_Storage
cross join (select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6)
as c(n) group by dbo.Data_Storage.id_datasource
Aaron C Miller - MO
vonpato