I have a table say
+--------+-----+-------------+-----------+
| ID | REV | Description | curr |
+--------+-----+-------------+-----------+
| 211-32 | 001 | Screw | READY |
| 211-32 | 002 | Screw_2 | NULL |
| 212-41 | 001 | bolt | READY |
| 212-41 | 002 | bolt_v2 | READY |
| 423-98 | 001 | Nut | WITHDRAWN |
| 423-98 | 002 | Nut_2 | NULL |
+--------+-----+-------------+-----------+
I want to take the ID with latest revision from this. But if the curr is "NULL" then i have to take the previous row and if the previous curr is WITHDRAWN , I don't need that ID itself. So my expected output is like below
+--------+-----+-------------+-------+
| ID | REV | Description | curr |
+--------+-----+-------------+-------+
| 211-32 | 001 | Screw | READY |
+--------+-----+-------------+-------+
| 212-41 | 002 | BOLT_2 | READY |
+--------+-----+-------------+-------+
I have tried the below query using temp table but it is not giving all rows.
select *,dense_rank() over (partition by id order by rev desc) as DR
into #material_DN
from material
select * from #material_DN where DR = case when curr='NULL' then 2 else 1 end