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 2008 Forums
 Transact-SQL (2008)
 How to take the max but exclude a row?

Author  Topic 

phrankbooth
Posting Yak Master

162 Posts

Posted - 2014-02-28 : 23:57:58
In this table:
select * into #tmp
from
(
select 'abc' a, 'xyz' b, 10 c union all
select 'abc' a, 'ghi' b, 20 c union all
select 'abc' a, 'aaa' b, 30 c ) t

How can you group it by column a, b take max(c), BUT exclude rows with b='aaa'??

So that the result is 20.

Thanks


--PhB

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-03-01 : 00:05:10
[code]
select max(c)
from #tmp
where b <> 'aaa'
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

phrankbooth
Posting Yak Master

162 Posts

Posted - 2014-03-01 : 00:21:12
thanks for your reply.

sorry, I should have been clearer.

I simplified the table, but I talked about grouping it because 'aaa' is not the only field value, there will be more rows with other groupings besides 'abc'

--PhB
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-03-01 : 00:31:22
then can you illustrate further on your requirement ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -