Skip to content

Instantly share code, notes, and snippets.

@barrychapman
Created March 7, 2016 16:02
Show Gist options
  • Save barrychapman/a2df2367fb67e35a53f8 to your computer and use it in GitHub Desktop.
Save barrychapman/a2df2367fb67e35a53f8 to your computer and use it in GitHub Desktop.
SELECT
(SELECT
SUM(Zones2.impressions_count) AS `impressions_count`
FROM
zones Zones
WHERE
Zones.location_id = Locations.id) AS `impressions_count`,
Retailers.id AS `Retailers__id`,
Retailers.account_rep_id AS `Retailers__account_rep_id`,
Retailers.leadsource_id AS `Retailers__leadsource_id`,
Retailers.industry_id AS `Retailers__industry_id`,
Retailers.name AS `Retailers__name`,
Retailers.billing_street1 AS `Retailers__billing_street1`,
Retailers.billing_street2 AS `Retailers__billing_street2`,
Retailers.billing_city AS `Retailers__billing_city`,
Retailers.billing_region_id AS `Retailers__billing_region_id`,
Retailers.billing_postal_code AS `Retailers__billing_postal_code`,
Retailers.billing_country_id AS `Retailers__billing_country_id`,
Retailers.main_phone AS `Retailers__main_phone`,
Retailers.locations_count AS `Retailers__locations_count`,
Retailers.contacts_count AS `Retailers__contacts_count`,
Retailers.created AS `Retailers__created`,
Retailers.modified AS `Retailers__modified`
FROM
retailers Retailers
INNER JOIN
locations Locations ON Locations.retailer_id = Retailers.id
INNER JOIN
zones Zones2 ON Zones2.location_id = Locations.id
LEFT JOIN
industries Industries ON Industries.id = (Retailers.industry_id)
LEFT JOIN
lead_sources LeadSources ON LeadSources.id = (Retailers.leadsource_id)
LEFT JOIN
users AccountRepresentative ON AccountRepresentative.id = (Retailers.account_rep_id)
LEFT JOIN
profiles Profiles ON AccountRepresentative.id = (Profiles.users_id)
GROUP BY Retailers.id
LIMIT 15 OFFSET 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment