Author |
Topic |
Lopaka
Starting Member
48 Posts |
Posted - 2007-11-06 : 13:58:42
|
I am trying to get the Min and/or Max of Three Columns in a record. Is there an easy way to do this?Examplefld1, fld2, fld310, 15, 20 <= Min=10, Max(20)Thank youRobert R. Barnes |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-06 : 14:11:07
|
It depends. Do you also have a identity column on the table? E 12°55'05.25"N 56°04'39.16" |
|
|
Lopaka
Starting Member
48 Posts |
Posted - 2007-11-06 : 14:33:37
|
There is no id column... I do not see why you would need one to find the min/max of a recordset? I am currently using a (case) function to determine the min/max. was hoping there was an easier way.select minfld = (case when fld1<fld2 and fld1<fld3 then fld1when fld2<fld1 and fld2<fld3 then fld2when fld3<fld1 and fld3<fld2 then fld3 end),maxfld = = (case when fld1>fld2 and fld1>fld3 then fld1when fld2>fld1 and fld2>fld3 then fld2when fld3>fld1 and fld3>fld2 then fld3 end)Robert R. Barnes |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
Reporter
Starting Member
48 Posts |
Posted - 2007-11-07 : 05:23:08
|
declare @t table (f1 int,f2 int,f3 int)insert @tselect 1,2,3 union allselect 4,2,3 union allselect 1,6,3 union allselect 7,2,7 union allselect 9,4,3select minval=case when f1<=f2 and f1<=f3 then f1 else case when f2<=f3 then f2 else f3 end end,maxval=case when f1>=f2 and f1>=f3 then f1 else case when f2>=f3 then f2 else f3 end endfrom @t |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-07 : 06:02:42
|
That is a nice summary of the link MVj provided. E 12°55'05.25"N 56°04'39.16" |
|
|
Reporter
Starting Member
48 Posts |
Posted - 2007-11-08 : 04:11:48
|
haha :)How about something like...declare @t table (f1 float,f2 float,f3 float)insert @tselect 1,2,8 union allselect 4,3,2 union allselect 3,9,5 union allselect 7,6,0 union allselect 1,5,7selectminval=0.25*((f1+f2+2*f3-abs(f1-f2))-abs(f1+f2-2*f3-abs(f1-f2))),maxval=0.25*((f1+f2+2*f3+abs(f1-f2))+abs(f1+f2-2*f3+abs(f1-f2))),from @tHow you can see this select does not contains any case statment.And It's possible to do the same for not only three fields... |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-08 : 04:35:54
|
Nice trick but much slower.Here is data from ProfilerRecords CPU READS method405769 2156 1661 formula405769 532 1661 case E 12°55'05.25"N 56°04'39.16" |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-11-08 : 09:50:48
|
No one ever shows how to deal with the possibility of one or more of the columns being null, but in this type of table design it almost a certain that some of the columns will be nullable.I think it will be very hard to implement that formula from Reporter with nullable columns. select [Min_of_fld1_to_fld3_Method_1] = ( select X1= min(bb.xx) from ( select xx = a.fld1 where a.fld1 is not null union all select xx = a.fld2 where a.fld2 is not null union all select xx = a.fld3 where a.fld3 is not null ) bb ) , [Min_of_fld1_to_fld3_Method_2] = case when a.fld1 is not null and (a.fld1 <= a.fld2 or a.fld2 is null) and (a.fld1 <= a.fld3 or a.fld3 is null) then a.fld1 when a.fld2 is not null and (a.fld2 <= a.fld1 or a.fld1 is null) and (a.fld2 <= a.fld3 or a.fld3 is null) then a.fld2 when a.fld3 is not null and (a.fld3 <= a.fld1 or a.fld1 is null) and (a.fld3 <= a.fld2 or a.fld2 is null) then a.fld3 else null endfrom MyTable a CODO ERGO SUM |
|
|
|