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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 is highest number

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 number
fred 12
bob 15

how can I add another field to show which one is the highest number ?
something like :

name number ishighest
fred 12 0
bob 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 used


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jamie
Aged Yak Warrior

542 Posts

Posted - 2008-10-16 : 08:58:09
select name, number from [users]
Go to Top of Page

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]


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jamie
Aged Yak Warrior

542 Posts

Posted - 2008-10-16 : 09:24:19
amazing.
thank you !
Go to Top of Page

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.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-16 : 09:52:46
I forget that this is posted in SQL Server 2005 forum
Other method

select *,case when max(number) over () =number then 1 else 0 end as ishighest from users


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -