Created
September 26, 2018 00:12
-
-
Save chris-muller/3409be1cc92ecdcfb5fb256dd5782410 to your computer and use it in GitHub Desktop.
SQL View that has a conditional column, simplifying other complex queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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