Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SputterPuttRedux/e1bde0f91a4c4c66c184 to your computer and use it in GitHub Desktop.
Save SputterPuttRedux/e1bde0f91a4c4c66c184 to your computer and use it in GitHub Desktop.
CREATE VIEW v_dynamic_pricing_for_accounts AS
SELECT
dynamic_pricing_bid_infos.account_id,
dynamic_pricing_bid_infos.exclusive_lead_status,
dynamic_pricing_bid_infos.call_status,
array_agg(profiles.id) profile_ids,
accounts.pfp_start_date pfp_inception_date,
accounts.rep_user_id,
accounts.pfp_budget pfp_budget,
max_activity_prices.shared_lead_buyers
FROM dynamic_pricing_bid_infos
JOIN profiles ON dynamic_pricing_bid_infos.account_id = profiles.account_id
JOIN accounts ON dynamic_pricing_bid_infos.account_id = accounts.id
JOIN activity_prices ON dynamic_pricing_bid_infos.account_id = activity_prices.account_id
JOIN
(
SELECT activity_prices.account_id account_id, activity_prices.exclusive_leads_enabled shared_lead_buyers, max(activity_prices.updated_at)
FROM activity_prices
GROUP BY activity_prices.account_id,
activity_prices.exclusive_leads_enabled
) AS max_activity_prices
ON max_activity_prices.account_id = dynamic_pricing_bid_infos.account_id
-- WHERE dynamic_pricing_bid_infos.account_id = max_activity_price.account_id
GROUP BY
dynamic_pricing_bid_infos.account_id,
accounts.pfp_start_date,
accounts.rep_user_id,
accounts.pfp_budget,
dynamic_pricing_bid_infos.call_status,
dynamic_pricing_bid_infos.exclusive_lead_status,
activity_prices.account_id,
max_activity_prices.shared_lead_buyers
limit 10;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment