Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Newest record with particular status?

Status
Not open for further replies.

pelajhia

Programmer
May 19, 1999
592
US
I have two tables, table A contains date, type & transid
table B contains invoiceid and transid.

I need to get a count of invoices from table b by day where the newest transaction in table a is of type='M' (grouping by tableb.headerid).

My basic select to find transactions containing M types is:

select a.type, a.date, b.invoiceid
from
a, b
where a.transid=b.transid
and a.type='M'

After this point, it gets pretty foggy for me:

Throwing in a 'group by invoiceid' seems to keep me from being able to get the actual a.type from the table, rather I am forced to do something like count, etc.

eg:
select count(a.type), max(a.date), b.invoiceid
from a,b
where a.transid=b.transid
and a.type='M'
group by b.invoiceid

I was thinking I could create a loop to read through each grouping, but I'm just at a loss as to where to start. Any help would be greatly appreciated!!
Thanks.
 
Code:
select a.type, a.date, cb.invoiceid
from
a ,  b as cb
where a.transid=cb.transid
and a.type='M'
and cb.headerid in (select max(headerid) from b
 where b.transid = cb.transid)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top