I have two tables, Plan
and Plan_Log
. Their structure looks like below:
Table: Plan
Id PlanName PlanApprovalDate PlanStatus
-- -------- ----------------------- ----------
1 abc 18-04-2014 15:48:22.790 A
2 def 19-07-2014 15:48:22.790 A
Table: Plan_Log
Id PlanName PlanApprovalDate
-- -------- -----------------------
1 abc 18-04-2014 15:48:22.790
2 abc 17-04-2014 15:48:22.790
3 abc 15-04-2014 15:48:22.790
4 def 19-07-2014 15:48:22.790
5 def 18-07-2014 15:48:22.790
6 def 17-07-2014 15:48:22.790
From these records I want to retrieve each plan with its two most recent PlanApprovalDate
records. Also, I need to make sure that these plans are active ones (PlanStatus
). So my final list should retrieve:
ID PlanName PlanApprovalDate
-- -------- -----------------------
1 abc 18-04-2014 15:48:22.790
2 abc 17-04-2014 15:48:22.790
3 def 18-07-2014 15:48:22.790
4 def 19-07-2014 15:48:22.790
How can I achieve this result?
I am using Sybase ASE 12.5.4