Home All Groups Group Topic Archive Search About

Selecting MAX per group

Author
20 Aug 2005 12:40 AM
Dave
I have the following table and I need to extract the maximum price for each
billitem (i.e., A-69 and B-174).

Can anyone give me some pointers on how to do this in T-SQL?

billitem    itemquantity    unitprice
A       4    41.0000
A       4    69.0000
B       4    97.0000
B       4    174.0000

---------
IF object_id('tempdb..#tmp') IS NOT NULL
    DROP TABLE #tmp
CREATE TABLE #tmp
    (billitem char(4)
    ,itemquantity int
    ,unitprice money)
GO
INSERT #tmp (billitem,itemquantity,unitprice) VALUES('A',4, 41.00)
INSERT #tmp (billitem,itemquantity,unitprice) VALUES('A',4, 69.00)
INSERT #tmp (billitem,itemquantity,unitprice) VALUES('B',4, 97.00)
INSERT #tmp (billitem,itemquantity,unitprice) VALUES('B',4, 174.00)
GO
SELECT * FROM #tmp

Author
20 Aug 2005 1:00 AM
Tom Moreau
Try:

select
billitem
, max (unitprice)
from
#tmp
group by
billitem


BTW, thanx for the DDL.  It made helping you quite easy.  :-)

--
   Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada
www.pinpub.com
..
"Dave" <D***@discussions.microsoft.com> wrote in message
news:E93DCC58-FEFE-4148-9407-4E1BA6A1564A@microsoft.com...
I have the following table and I need to extract the maximum price for each
billitem (i.e., A-69 and B-174).

Can anyone give me some pointers on how to do this in T-SQL?

billitem itemquantity unitprice
A   4 41.0000
A   4 69.0000
B   4 97.0000
B   4 174.0000

---------
IF object_id('tempdb..#tmp') IS NOT NULL
DROP TABLE #tmp
CREATE TABLE #tmp
(billitem char(4)
,itemquantity int
,unitprice money)
GO
INSERT #tmp (billitem,itemquantity,unitprice) VALUES('A',4, 41.00)
INSERT #tmp (billitem,itemquantity,unitprice) VALUES('A',4, 69.00)
INSERT #tmp (billitem,itemquantity,unitprice) VALUES('B',4, 97.00)
INSERT #tmp (billitem,itemquantity,unitprice) VALUES('B',4, 174.00)
GO
SELECT * FROM #tmp

AddThis Social Bookmark Button