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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Latest Record for each Ref field
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tim8w
Starting Member

USA
10 Posts

Posted - 02/19/2014 :  13:53:55  Show Profile  Reply with Quote
I have a table that contains a Ref field and a TransactionDate field. For each Ref field there are mutliple Transactions. I am trying to put together a query that selects the Latest Transaction date for each Ref field in the table.

I tried:

SELECT *
FROM       tabMediaTransaction
WHERE     (TransactionDate =
          (SELECT     MAX(TransactionDate) AS Expr1
           FROM          tabMediaTransaction AS tabMediaTransaction_1))


But this only returns 1 record for 1 Ref field. I need 1 record for each Ref field

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1600 Posts

Posted - 02/19/2014 :  14:28:38  Show Profile  Reply with Quote
select a.*
from (
	SELECT *, row_number() over (partition by Ref order by TransactionDate DESC) rn
	FROM       tabMediaTransaction
	) a
where a.rn = 1


===============================================================================
There are two kinds of light -- the glow that illuminates, and the glare that obscures. -James Thurber (1894-1961)

Edited by - Bustaz Kool on 02/19/2014 14:30:51
Go to Top of Page

tim8w
Starting Member

USA
10 Posts

Posted - 02/19/2014 :  15:02:07  Show Profile  Reply with Quote
Function row_number() is not recognized...
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
5944 Posts

Posted - 02/19/2014 :  15:15:07  Show Profile  Reply with Quote
MS sql server, right? which version are you using?

Be One with the Optimizer
TG
Go to Top of Page

tim8w
Starting Member

USA
10 Posts

Posted - 02/19/2014 :  15:17:17  Show Profile  Reply with Quote
2008
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
5944 Posts

Posted - 02/19/2014 :  15:21:15  Show Profile  Reply with Quote
Please check the database compatibility level.
<right-click database in object explorer> | properties | Options

Be One with the Optimizer
TG
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
5944 Posts

Posted - 02/19/2014 :  15:22:16  Show Profile  Reply with Quote
try this if your compatibility level is below 2005:

SELECT t.*
FROM   tabMediaTransaction t
inner join (
       select ref, max(transactionDate) maxtdate
       from   tabMediaTransaction
       group by ref
       ) d
              on d.ref = t.ref
              and d.maxtdate = t.TransactionDate

This could work too - see which performs better:

select *
FROM   tabMediaTransaction t
where exists (
       select 1
       from tabMediaTransaction
       where ref = t.ref
       having max(transactiondate) = t.transactionDate
       )

Be One with the Optimizer
TG

Edited by - TG on 02/19/2014 15:29:13
Go to Top of Page

tim8w
Starting Member

USA
10 Posts

Posted - 02/19/2014 :  17:29:47  Show Profile  Reply with Quote
That worked... Thanks!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000