| Author |
Topic |
|
Clages1
Yak Posting Veteran
69 Posts |
Posted - 2007-01-27 : 10:02:06
|
| Hi, i have a table Named PRICE like thisCode Price1 , Price2 , Price3, Price4A 100 80 50 20b 120 90 0 0C 150 0 0 0D 200 45 20 0 In fact any combination, but Price1 is the highest and Price4 theLowesti would like to do a UPdate changing toA 20 50 80 100b 90 120 0 0C 150 0 0 0D 20 45 200 I was trying using case, but i can not get right resultsany help will be apreciatedTksC Lages |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-01-27 : 14:38:06
|
| I don't understand your requirements. What determines the order the values in the output, and why does the last row have a column missing?CODO ERGO SUM |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-01-28 : 01:44:15
|
[code]-- create the function that perform the sortingcreate function f_sort( @pos int, @val1 int, @val2 int, @val3 int, @val4 int)returns intasbegin declare @t table ( id int identity(1,1), value int ) insert into @t(value) select value from ( select @val1 as value union all select @val2 as value union all select @val3 as value union all select @val4 ) v order by case when value <> 0 then 1 else 2 end, value return (select value from @t where id = @pos)endgo[/code] [code]-- create table for testingdeclare @price table( Code varchar(10), Price1 int, Price2 int, Price3 int, Price4 int)-- insert test data into table for testinginsert into @priceselect 'A', 100, 80, 50, 20 union allselect 'B', 120, 90, 0, 0 union allselect 'C', 150, 0, 0, 0 union allselect 'D', 200, 45, 20, 0 -- Original dataselect * from @price/*Code Price1 Price2 Price3 Price4 ---------- ----------- ----------- ----------- ----------- A 100 80 50 20B 120 90 0 0C 150 0 0 0D 200 45 20 0*/-- Perform the updateupdate pset Price1 = dbo.f_sort(1, Price1, Price2, Price3, Price4), Price2 = dbo.f_sort(2, Price1, Price2, Price3, Price4), Price3 = dbo.f_sort(3, Price1, Price2, Price3, Price4), Price4 = dbo.f_sort(4, Price1, Price2, Price3, Price4)from @price p-- After updateselect * from @price/*Code Price1 Price2 Price3 Price4 ---------- ----------- ----------- ----------- ----------- A 20 50 80 100B 90 120 0 0C 150 0 0 0D 20 45 200 0*/[/code] KH |
 |
|
|
Clages1
Yak Posting Veteran
69 Posts |
Posted - 2007-01-29 : 11:39:12
|
| KthanTks for the answeryou help me loti did some adjustment,butyour query did what i needtksagainC. Lages |
 |
|
|
|
|
|