Skip to content

Instantly share code, notes, and snippets.

@nicnilov
Created June 16, 2015 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicnilov/5272e822ca146f3c50e9 to your computer and use it in GitHub Desktop.
Save nicnilov/5272e822ca146f3c50e9 to your computer and use it in GitHub Desktop.
SELECT "customers".*
FROM (
SELECT "customers".*
FROM "customers"
WHERE "customers"."company_id" = $1
AND (
NOT (
EXISTS (
SELECT "import_logs".*
FROM "import_logs"
WHERE "import_logs"."importable_type" = 'Customer'
AND "import_logs"."importable_id" = "customers"."id"
AND "import_logs"."remote_type" = 'account'
)
)
)
UNION
SELECT "customers".*
FROM "customers"
INNER JOIN "import_logs"
ON "import_logs"."importable_id" = "customers"."id"
AND "import_logs"."importable_type" = 'Customer'
WHERE "customers"."company_id" = $1
AND "import_logs"."importable_type" = 'Customer'
AND "import_logs"."remote_type" = 'account'
AND "import_logs"."status" = 'pending'
) "customers"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment