I have data that where most of the columns have unique data. There are only three columns from the table that I am interested in and two of them have unique data.
Example data:
Ins_Cd | Encounter | Date
-------------------------------
A00 | 12345678 | 01-01-2001
A00 | 98765432 | 02-01-2001
From the above I want to return the second record
Ins_Cd | Encounter | Date
-------------------------------
A00 | 98765432 | 02-01-2001
I wrote the following code, which I think can be improved upon. It runs fairly quickly ~ 9 seconds whith just under 2 million records in the view.
SELECT Pyr1_Co_Plan_Cd
, PtNo_Num
, Dsch_Date
, [rn] = ROW_NUMBER() over(
partition by pyr1_co_plan_cd
order by dsch_date desc
)
into #temp
FROM schema.my_view
where Med_Rec_No is not null
and Dsch_Date is not null
and LEFT(PtNo_Num, 1) != '2'
and LEFT(ptno_num, 4) != '1999'
and LEFT(ptno_num, 1) != '9'
order by Pyr1_Co_Plan_Cd
, Dsch_Date desc
;
select a.Pyr1_Co_Plan_Cd
, a.PtNo_Num
, a.Dsch_Date
from #temp as a
where a.rn = 1
order by a.Pyr1_Co_Plan_Cd
;
drop table #temp
;
The above does give me what I want. How can I write this a bit more efficiently? Or should I be posting this on codereview