|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Selecting MAX per groupbillitem (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 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 I have the following table and I need to extract the maximum price for eachnews:E93DCC58-FEFE-4148-9407-4E1BA6A1564A@microsoft.com... 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
Other interesting topics
|
|||||||||||||||||||||||