Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to take the max but exclude a row?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

phrankbooth
Posting Yak Master

USA
162 Posts

Posted - 02/28/2014 :  23:57:58  Show Profile  Reply with Quote
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)

Singapore
17689 Posts

Posted - 03/01/2014 :  00:05:10  Show Profile  Reply with Quote

select  max(c)
from    #tmp
where   b <> 'aaa'



KH
Time is always against us

Go to Top of Page

phrankbooth
Posting Yak Master

USA
162 Posts

Posted - 03/01/2014 :  00:21:12  Show Profile  Reply with Quote
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)

Singapore
17689 Posts

Posted - 03/01/2014 :  00:31:22  Show Profile  Reply with Quote
then can you illustrate further on your requirement ?


KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000