Untitled

From Alex, 4 Months ago, written in Plain Text, viewed 58 times. This paste will hop the perch in 1 Second.
URL http://codebin.org/view/24204712 Embed
Download Paste or View Raw
  1. WITH monthly_funds AS(
  2.     SELECT q_month AS month,
  3.            COUNT(DISTINCT name) AS uniq_fund_qty
  4.     FROM(
  5.         SELECT EXTRACT(MONTH from fr.funded_at) AS q_month,
  6.                fr.funded_at,
  7.                f.name
  8.         FROM funding_round AS fr
  9.         LEFT JOIN investment as i ON fr.id=funding_round_id
  10.         LEFT JOIN fund AS f ON f.id=i.fund_id
  11.         WHERE f.country_code = 'USA'
  12.           AND EXTRACT(YEAR from fr.funded_at) IN (2010, 2011, 2012, 2013)
  13.         ) AS q
  14.      GROUP BY q_month
  15. ),
  16. monthly_aquired_companies AS(
  17.        SELECT q_month,
  18.        count(companies_aquired) AS companies_aquired_qty,
  19.        sum(deal_sum) AS deals_sum
  20.        FROM(SELECT EXTRACT(MONTH from a.acquired_at) AS q_month,
  21.                    acquired_company_id AS companies_aquired,
  22.                    price_amount AS deal_sum
  23.             FROM acquisition AS a
  24.             WHERE EXTRACT(YEAR from a.acquired_at) IN (2010, 2011, 2012, 2013)
  25.             GROUP by EXTRACT(MONTH from a.acquired_at), acquired_company_id,a.price_amount
  26.             ORDER BY EXTRACT(MONTH from a.acquired_at)
  27.            ) AS q
  28.        GROUP BY q_month                          
  29. )
  30. SELECT month,
  31.        uniq_fund_qty,
  32.        companies_aquired_qty,
  33.        deals_sum
  34. FROM monthly_funds AS mf
  35. JOIN monthly_aquired_companies AS mac ON mf.month=mac.q_month

Reply to "Untitled"

Here you can reply to the paste above