outcome logic sql