SQL Задача 31

From Martin Movsisyan, 2 Months ago, written in SQL, viewed 90 times.
URL http://codebin.org/view/38ee7b21 Embed
Download Paste or View Raw
  1. WITH profile AS
  2. (SELECT customer_id
  3.         ,MIN(CAST(DATE_TRUNC('week', order_date) AS TIMESTAMP)) AS dt
  4.         ,COUNT(*) OVER (PARTITION BY MIN(CAST(DATE_TRUNC('week', order_date) AS TIMESTAMP))) AS cohort_users_cnt
  5. FROM northwind.orders
  6. GROUP BY customer_id
  7. ORDER BY 2),
  8.      sessions AS
  9. (SELECT customer_id,
  10.        CAST(DATE_TRUNC('week', order_date) AS TIMESTAMP) AS session_date
  11. FROM northwind.orders
  12. GROUP BY customer_id, order_date
  13. ORDER BY 2,1)
  14.  
  15. SELECT p.dt AS cohort_dt,
  16.        s.session_date AS purchase_date,
  17.        COUNT(p.customer_id) AS users_cnt,
  18.        p.cohort_users_cnt,
  19.        ROUND(COUNT(p.customer_id) * 100.0 / p.cohort_users_cnt, 2) AS retention_rate
  20. FROM profile p
  21. JOIN sessions s ON p.customer_id = s.customer_id
  22. GROUP BY 1,2,4
  23. ORDER BY 1,2

Replies to SQL Задача 31 rss

Title Name Language When
Re: SQL Задача 35 Martin Movsisyan sql 2 Months ago.

Reply to "SQL Задача 31"

Here you can reply to the paste above