Author |
Topic |
jayram11
Yak Posting Veteran
97 Posts |
Posted - 2011-05-23 : 14:29:59
|
Hello I have asked this Q earlier and haven't been able to progress much from the information pointed to me.i want to rearrange and update a table inside a recordcreate table #sort(PR varchar(5),val1 varchar (2),val2 varchar (2),val3 varchar (2),)insert into #sort(PR , val1, val2, val3)select 2345X , ‘AU’, ‘ZZ’, ‘BK’ union allselect 2345Y , ‘AU’, ‘KX, ‘BK’ union allselect 2345Y , ‘AU’, ‘BZ, ‘BK’ union allselect 2345Y , ‘XY’, ‘ZZ’, ‘BK’ union allselect 2345A , ‘AB’, ‘AA’, ‘BK’ union allselect 2345A , ‘AU’, ‘ZZ’, ‘BK’ union allselect 2345A , ‘AU’, ‘ZZ’, ‘BK’ union allselect 2345B , ‘AU’, ‘ZZ’, ‘’ union allselect 2345B , ‘AU’, ‘’, ‘BK’ union allselect 2345B , ‘AU’, ‘ZZ’, ‘BK’ union allselect 2345C, ‘AU’, ‘’, ‘AA’ union allselect 2345C , ‘AU’, ‘ZZ’, ‘BK’ union allselect 2345C , ‘’, ‘ZZ’, ‘BK’ union allselect 2345D , ‘AU’, ‘ZZ’, ‘’this is kind of unique so was wondering i anyone could help mei want to sort the VAL variables horizontally in alphabetical orderif it is val1, val2 and val3 of ‘’, ‘ZZ’, ‘BK’, i need to sort to ‘BK’, ‘ZZ’, '' and update val1, val2 and val3 to ‘BK’, ‘ZZ’, ''if it val1, val2 and val3 of 'AB', 'AA', 'AC', i need to sort to 'AA', 'AB', 'AC' and update val1, val2 and val3 to 'AA', 'AB', 'AC'empty values ('') should always be in last variable - Val3All help is appreciated. ROBVOLK pointed me to an older post but i couldn't develop much from itThanks |
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2011-05-23 : 16:22:04
|
please provide desired output, based on your DDL.thank you |
 |
|
Andrew Zwicker
Starting Member
11 Posts |
Posted - 2011-05-23 : 22:25:57
|
Hi,This should work, assuming your table only has 3 value columns. If three value columns was simply used as an example and the real table could have more, then a more general solution would be needed. The update statement after the main one is needed because sql considers blanks to be before, not after, other strings, and the query would get much messier trying to handle this in the main query.update s set val1 = (case when val1 < val2 and val1 < val3 then val1 when val2 < val3 and val2 < val1 then val2 else val3 end) -- as 'MinValue', ,val2 = (case when (val1 > val2 and val1 < val3) or (val1 < val2 and val1 > val3) then val1 when (val2 > val1 and val2 < val3) or (val2 < val1 and val2 > val3) then val2 else val3 end) -- as 'MiddleValue', ,val3 = (case when val1 > val2 and val1 > val3 then val1 when val2 > val3 and val2 > val1 then val2 else val3 end) -- as 'maxValue'from #sort s--Run 2 times if a record could have two blanks:update #sort set val1 = val2, val2 = val3, val3 = val1 where isnull(val1, '') = '' I hope this helps.- Andrew ZwickerVisit http://www.helpwithsql.com |
 |
|
|
|
|