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)
 latest record query?

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-03-04 : 12:10:30
Greetings

Hitting a wall here after many trial and error. I have the following invoice table
Invoice
InvoiceID,
WoNumber,
OrderDate

WoNumber can be repeated , meaning there could be identical wonumbers (they recycle wonumbers)
how can I show the latest record from this table
.
I have tried the following
select * 
From Invoice e
inner join (select WoNumber,invoiceid, max(orderdate) from invoice group by WoNumber,invoiceid) t
on e.invoiceid = t.invoiceid

I wish to use row_number() thingy but that is way above my league.
Appreciate your help

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-03-04 : 12:14:34
quote:
Originally posted by yosiasz

Greetings

Hitting a wall here after many trial and error. I have the following invoice table
Invoice
InvoiceID,
WoNumber,
OrderDate

WoNumber can be repeated , meaning there could be identical wonumbers (they recycle wonumbers)
how can I show the latest record from this table
.
I have tried the following
select * 
From Invoice e
inner join (select WoNumber,max(orderdate)as MAXORDER from invoice group by WoNumber) t
on e.orderdate = t.MAXORDER and e.WoNumber = t.Wonumber

I wish to use row_number() thingy but that is way above my league.
Appreciate your help

Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-03-04 : 12:22:03
select t.InvoiceID,
t.WoNumber,
t.OrderDate
from
( select row_number() over ( partition by wonumber order by orderdate desc ) as sno,InvoiceID,
WoNumber,
OrderDate
from Invoice ) t
where t.sno =1
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-03-04 : 12:38:26
thank you very much...!
Go to Top of Page
   

- Advertisement -