Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Postgresql querying records where the previous record already in completed status

based on the following data in the database:

enter image description here

i would like to query for all the records where status = null, but the status of the previous step_no is "done" group by the order.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

appreciate some guidance on what sql can do so.

>Solution :

You can use lag() function like below:


with cte as (
select 
*, 
lag(status,1) over ( order by "order", "step") "previous" 
from demo
)
select 
"order", 
step, 
status 
from cte where status is null and previous='done'

DEMO

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading