This feels like a case for a function.
See my reply to hn_throwaway_99. The example is overly trivialized and should expand to more complex relational logic as well.