I have a table Slot and SlotAlteration.
The slot has a time_off_id field.
SlotAlteration has two fields, slot_id and action. Action can be 0 or 1. Each slot can have up to two entries in the SlotAlteration table with action 0 and 1 (not 0 and 0, and not 1 and 1) or have no connections to SlotAdjustment at all.
I need to select all records from the Slot table where time_off_id is null, and if there is no record in SlotAlteration with such slot_id and action 1
So, if time_off_id is null and if there is no row in SlotAdjustment with action 1.
I wrote this query, but it does not work that way i needed.
SELECT * FROM Slot LEFT OUTER JOIN SlotAdjustment ON Slot.id = SlotAdjustment.slot_id WHERE time_off_id IS null AND (SlotAdjustment.id is null or SlotAdjustment.action = 0);
This is sandbox with example database
https://sqlfiddle.com/postgresql/online-compiler?id=41ee3a0f-0b28-434d-a54c-8b9b8320a0af