I had difficulties to even come up with the correct question title. I'll take any advice to edit it.
My problem: I have an app (job board) with two models: User, Job. User can have many jobs and jobs can be premium
or free
. Jobs can be expired or active as well. They are active if valid_until
parameter IS NOT NULL
, otherwise they are expired (I have a background job to set this automatically every day).
I am trying to select Users, who have active jobs but I want users with premium jobs come before users with free jobs assuming users can have active both premium and free job postings at the same time.
SELECT DISTINCT users.id, users.email FROM users
LEFT OUTER JOIN jobs ON jobs.owner_id = users.id
WHERE jobs.valid_until IS NOT NULL;
Above query correctly selects users with at least one active job (either premium or free), I just need to order users according to jobs table. I will appreciate any help/hint. Thanks!