2

I have a structure role_approvers:

id, role_id, level, user_id, approval_date

Rows:

4116, 1300, 1, 1158, 2021-05-28 14:24:34
4117, 1300, 1, 1186, NULL
4596, 1300, 2, 21, NULL

I need to get MIN level where role_id = 1300 and ALL approval_date on this level should be NULL

So in this case I need to get level = 2, but I don't know how to get it, can someone help with a query

forpas
  • 160,666
  • 10
  • 38
  • 76

1 Answers1

1

You can filter the table for role_id = 1300, group by level, set the condition in the HAVING clause that all dates are null and return the top level:

SELECT level
FROM role_approvers
WHERE role_id = 1300
GROUP BY level
HAVING MAX(approval_date) IS NULL
ORDER BY level LIMIT 1

See the demo.

forpas
  • 160,666
  • 10
  • 38
  • 76