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.
| Author |
Topic |
|
sreenu9f
Yak Posting Veteran
73 Posts |
Posted - 2010-06-16 : 18:25:06
|
| I have a table like this AcctNo Code Weight Amount123 pxx 4.5 120123 pxy 4.3 100234 a23 1.2 50234 b13 1.2 25456 c13 2.2 50456 d32 2.2 50I would like to have only one Code for every given AcctNo based on the logic 1) check the weights and if the weights are different pick the code with the highest weight2) if the weights are same pick the code with the highest Amount3) if the weights and the Amount columns are same for an account then pick the first code.The result should look like this.AcctNo Code123 pxx234 a23456 c13please suggest. i need this ASAP.Thanks, |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-06-16 : 18:34:50
|
| [code]select AcctNo, Code from ( select *, row_number() over (partition by acctno order by acctno, weight desc) r from myTable ) awhere r=1[/code] |
 |
|
|
matty
Posting Yak Master
161 Posts |
Posted - 2010-06-17 : 01:55:58
|
a small change to meet condition 2quote: Originally posted by robvolk
select AcctNo, Code from ( select *, row_number() over (partition by acctno order by acctno, weight desc,amount desc) r from myTable ) awhere r=1
|
 |
|
|
sreenu9f
Yak Posting Veteran
73 Posts |
Posted - 2010-06-18 : 14:31:57
|
| Thank u all. It was very helpful. |
 |
|
|
|
|
|