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)
 SELECT highest value of duplicate field

Author  Topic 

magikminox
Starting Member

27 Posts

Posted - 2008-03-25 : 10:40:01
I have a table like
TradeID ActionID
58096 3663
58096 3664
78901 2235
78901 2236

I want to select the only the TradeID with the highest ActionID

I tried using
select distinct tradeid,actionid
From
cct
Where ActionID = (SELECT MAX(ActionID)
FROM cct1
WHERE cct1.TradeID = cct.TradeID)
group by tradeid,actionid

but the result is not correct

please help

In god we trust,everything else we test.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-25 : 10:45:01
select tradeid, actionid from (
select tradeid, actionid, row_number() over (partition by tradeid order by actioid desc) AS recid from table1
) as d where recid = 1



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-25 : 14:29:26
Also

SELECT TradeID,max(ActionID)
FROM cct
GROUP BY TradeID
Go to Top of Page

magikminox
Starting Member

27 Posts

Posted - 2008-03-26 : 04:06:17
Hi Peso
I dont quite understand your query.Pliz explain a bit or maybe clarify

thanks a lot

In god we trust,everything else we test.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-26 : 04:12:03
I can, if you first tell me which parts of Books Online you read and didn't understand about the ROW_NUMBER() function.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -