Skip to content

Instantly share code, notes, and snippets.

@chris-muller
Created September 26, 2018 00:12
Show Gist options
  • Save chris-muller/3409be1cc92ecdcfb5fb256dd5782410 to your computer and use it in GitHub Desktop.
Save chris-muller/3409be1cc92ecdcfb5fb256dd5782410 to your computer and use it in GitHub Desktop.
SQL View that has a conditional column, simplifying other complex queries
CREATE VIEW "view_entries" AS
-- Approved Entries
SELECT
"entries"."id",
"entries"."awardId",
"entries"."categoryId",
"entries"."userId",
CASE
-- approval disabled
WHEN ("awards"."approvalJudging" = false) THEN true
-- approval enabled and default is approved
WHEN ("awards"."approvalJudging" = true AND "awards"."entryApprovalDefault" = true AND ("entries"."approved" = true OR "entries"."approved" IS NULL)) THEN true
-- approval enabled and default is rejected
WHEN ("awards"."approvalJudging" = true AND "awards"."entryApprovalDefault" = false AND ("entries"."approved" = true)) THEN true
-- entry is rejected
ELSE false
END AS approved,
"entries"."paid",
"entries"."submitted",
"entries"."unsubmitted",
"entries"."createdAt",
"entries"."updatedAt"
FROM "entries"
JOIN "awards"
ON "entries"."awardId" = "awards".id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment