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 |
|
jamie
Aged Yak Warrior
542 Posts |
Posted - 2008-10-16 : 08:49:10
|
| hi,I am running a query which shows the values like this :name numberfred 12bob 15how can I add another field to show which one is the highest number ?something like :name number ishighestfred 12 0bob 15 1?thank you for any help.jamie |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-16 : 08:51:25
|
| Post the query you usedMadhivananFailing to plan is Planning to fail |
 |
|
|
jamie
Aged Yak Warrior
542 Posts |
Posted - 2008-10-16 : 08:58:09
|
| select name, number from [users] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-16 : 09:21:59
|
| [code]select u.name,u.number, case when u.number=t.maxi then 1 else 0 end as ishighest from users as u cross join(select max(number) as maxi from users) as t[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
jamie
Aged Yak Warrior
542 Posts |
Posted - 2008-10-16 : 09:24:19
|
| amazing.thank you ! |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-10-16 : 09:26:32
|
| check out the RANK() function. Careful where you have duplicate 'number'. You might want to use DENSE_RANK or ROW_NUMBER depending on what you want. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-16 : 09:52:46
|
| I forget that this is posted in SQL Server 2005 forumOther methodselect *,case when max(number) over () =number then 1 else 0 end as ishighest from usersMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|