Skip to content

Instantly share code, notes, and snippets.

@kirkconnell
Created May 5, 2011 18:07
Show Gist options
  • Select an option

  • Save kirkconnell/957552 to your computer and use it in GitHub Desktop.

Select an option

Save kirkconnell/957552 to your computer and use it in GitHub Desktop.
Query for SNPS comparisons
SELECT
e.id,
e.company_name,
l.url_for AS location,
sn_entities.location_id,
sn_entities.sn_price,
ps_entities.ps_price,
p.monthly_price AS snps_price
FROM
entities e INNER JOIN
(SELECT
e.id AS id,
ag.location_id AS location_id,
p.monthly_price AS sn_price
FROM
entities e INNER JOIN
product_subscriptions ps ON (ps.entity_id = e.id) INNER JOIN
products p ON (ps.product_id = p.id) INNER JOIN
sites s ON (p.site_id = s.id) INNER JOIN
association_groups ag ON (ag.product_subscription_id = ps.id)
WHERE
s.name = 'sn') sn_entities ON (sn_entities.id = e.id) INNER JOIN
(SELECT
e.id AS id,
ag.location_id AS location_id,
p.monthly_price AS ps_price
FROM
entities e INNER JOIN
product_subscriptions ps ON (ps.entity_id = e.id) INNER JOIN
products p ON (ps.product_id = p.id) INNER JOIN
sites s ON (p.site_id = s.id) INNER JOIN
association_groups ag ON (ag.product_subscription_id = ps.id)
WHERE
s.name = 'ps') ps_entities ON (sn_entities.id = ps_entities.id AND sn_entities.location_id = ps_entities.location_id) INNER JOIN
locations l ON (sn_entities.location_id = l.id) INNER JOIN
locations_products lp ON (lp.location_id = l.id) INNER JOIN
products p ON (lp.product_id = p.id) INNER JOIN
sites s ON (p.site_id = s.id)
WHERE
s.name = 'snps'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment