Skip to content

Instantly share code, notes, and snippets.

@gaygenius
Created September 18, 2014 22:19
Show Gist options
  • Save gaygenius/1e57d165a77cae5df2cf to your computer and use it in GitHub Desktop.
Save gaygenius/1e57d165a77cae5df2cf to your computer and use it in GitHub Desktop.
Query to pick the "starting OES id" for ingress from MySQL
WITH
config_oes_ids AS (
SELECT (config->>'precedingOESId')::integer AS id, organization_id
FROM connections
GROUP BY organization_id
),
max_deliv_oes_ids AS (
SELECT max(id) as id, organization_id
FROM deliverables
GROUP BY organization_id
),
mashup AS (
SELECT COALESCE(md.id, cc.id) AS chosen_oes_id, md.id AS md, cc.id AS cc, c.organization_id
FROM connections c
LEFT JOIN config_oes_ids AS cc USING (organization_id)
LEFT JOIN max_deliv_oes_ids AS md USING (organization_id)
WHERE (c.config->>'active') = 'true'
)
SELECT * FROM mashup
WHERE chosen_oes_id IS NOT NULL
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment