Link to home
Start Free TrialLog in
Avatar of Kelvin Sparks
Kelvin SparksFlag for New Zealand

asked on

Return latest value

Hi Experts, I have a table - columns A,B,C are the primary key, column D is a date, and E is a varchar.

What I need is for eack PK, I want the data for column E returned for the latest date - leaving behing all earlier records for each PK. Try as I might, I just can't see it - been coding too long today.

Kelvin
Avatar of Alpha Au
Alpha Au
Flag of Hong Kong image

try this?

select A,B,C,D,E from table1

inner join 
(
select A,B,C, Max(D) as MAXD from table1
group by A,B,C
) maxvalue

where table1.A = maxvalue.A
and table1.B = maxvalue.B
and table1.C = maxvalue.C
and table1.D = maxvalue.MAXD

Open in new window

Avatar of Kelvin Sparks

ASKER

Code below - syntax error at WHERE that occurs after the MaxValue - error message is: Incorrect syntax near the keyword 'WHERE'
(SELECT wr.Priority FROM AQD.qaoc_Work_Request wr INNER JOIN (SELECT WR_Period, WR_Type, WR_Number, MAX(Target_Start_Date) as Maxp FROM AQD.qaoc_Work_Request GROUP BY WR_Period, WR_Type, WR_Number) MaxValue
						WHERE wr.WR_Period = MaxValue.WR_Period AND wr.WR_Type = MaxValue.WR_Type AND wr.WR_Number = MaxValue.WR_Number and wr.Target_Start_Date =  MaxValue.Maxp)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Alpha Au
Alpha Au
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome - thanks for the prompt help