SQL stored procedure return single record per ID with Max Date for that ID -
i having problem figuring out how particular result data , can't seem write query trick.
here have. have table [products] contains details of particular product , have second table [prodtrans] 2 in 1 many relationship on product id number field "prid" prodtrans table has date field each record of when created , price. issue having want build query take recent record each prid , return them list. tried using clause max(date) seems returning records recent date overall , skipping of prid's recent record not recent overall date.
i uncertain how should adjust criteria correct issue.
here have written far.
select * prodtrans transdate = max(transdate) order prid
i want return list of prid's on table recent transaction prid in case current pricing product is.
please know missing simple life of me can not remember is.
[update]
alright let me clarify need have output below give example dataset table
prid createdate cost margin
001 1/1/2000 25.00 2.10 005 2/11/2005 15.48 1.50 002 3/5/2005 8.49 0.23 001 2/10/2006 22.95 1.19
and on need list returns 1 instance of each prid must 1 recent date/time value. digging found out there no foreign key relationship on prid field manually entered (again legacy system trying patch did not set nor design it. have work around odd design) procedure being used output data set external application.
select p.prid, cost,margin prodtrans p join ( select max(transdate) maxdate, prid prodtrans group prid ) m on m.prid = p.prid , m.maxdate = p.transdate order p.prid
Comments
Post a Comment