Skip to content

Instantly share code, notes, and snippets.

@kalenjordan
Last active January 1, 2016 20:59
Show Gist options
  • Select an option

  • Save kalenjordan/8200459 to your computer and use it in GitHub Desktop.

Select an option

Save kalenjordan/8200459 to your computer and use it in GitHub Desktop.

Revisions

  1. kalenjordan revised this gist Dec 31, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion repurchase-rate.sql
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Test
    # The number of customers who haveve only purchased once from December 2012 to December 2013

    SELECT customers_in_segment
    FROM
  2. kalenjordan revised this gist Dec 31, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions repurchase-rate.sql
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Test

    SELECT customers_in_segment
    FROM
    (
  3. kalenjordan revised this gist Dec 31, 2013. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions repurchase-rate.sql
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    SELECT customers_in_segment
    FROM
    (
    @@ -21,9 +20,9 @@ UNION
    SELECT count(*)
    FROM
    (
    SELECT customer_id, min(created_at) AS first_order_at, count(*) AS order_count
    FROM sales_flat_order
    WHERE created_at < '2013-12-31'
    GROUP BY customer_id
    SELECT customer_id, min(created_at) AS first_order_at, count(*) AS order_count
    FROM sales_flat_order
    WHERE created_at < '2013-12-31'
    GROUP BY customer_id
    ) AS summary
    WHERE first_order_at BETWEEN '2012-12-01' AND '2012-12-31'
  4. kalenjordan revised this gist Dec 31, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion repurchase-rate.sql
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@

    # The number of customers who've only made one purchase
    SELECT customers_in_segment
    FROM
    (
  5. kalenjordan created this gist Dec 31, 2013.
    30 changes: 30 additions & 0 deletions repurchase-rate.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@

    # The number of customers who've only made one purchase
    SELECT customers_in_segment
    FROM
    (
    SELECT count(*) AS customers_in_segment, order_count
    FROM
    (
    SELECT customer_id, min(created_at) AS first_order_at, count(*) AS order_count
    FROM sales_flat_order
    WHERE created_at < '2013-12-31'
    GROUP BY customer_id
    ) AS summary
    WHERE first_order_at BETWEEN '2012-12-01' AND '2012-12-31'
    GROUP BY order_count
    ) AS summary2
    WHERE order_count = 1

    UNION

    # The total number of new customers in December 2012
    SELECT count(*)
    FROM
    (
    SELECT customer_id, min(created_at) AS first_order_at, count(*) AS order_count
    FROM sales_flat_order
    WHERE created_at < '2013-12-31'
    GROUP BY customer_id
    ) AS summary
    WHERE first_order_at BETWEEN '2012-12-01' AND '2012-12-31'