Задача 35

From Martin Movsisyan, 2 Months ago, written in SQL, viewed 63 times.
URL http://codebin.org/view/3234d6eb Embed
Download Paste or View Raw
  1. WITH a AS
  2. (SELECT CAST(DATE_TRUNC('month', order_date) AS DATE) AS MONTH
  3.        ,COUNT(DISTINCT customer_id) AS customers_this_month
  4. FROM northwind.orders
  5. WHERE CAST(DATE_TRUNC('month', order_date) AS DATE) BETWEEN '1996-07-01' AND '1998-05-31'
  6. GROUP BY CAST(DATE_TRUNC('month', order_date) AS DATE)),
  7. b AS
  8. (SELECT *
  9.          ,ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS row_cus
  10. FROM northwind.orders
  11. ORDER BY 1)
  12.  
  13. SELECT CAST(DATE_TRUNC('month', order_date) AS DATE), COUNT(row_cus)
  14. FROM b
  15. WHERE row_cus = 1
  16. GROUP BY CAST(DATE_TRUNC('month', order_date) AS DATE)
  17. ORDER BY 1
  18. --SELECT CAST(DATE_TRUNC('month', order_date) AS date) AS month
  19. --    ,SUM(COUNT(row_cus)) OVER (ORDER BY CAST(DATE_TRUNC('month', order_date) AS date)) AS total_customers
  20. --FROM b
  21. --GROUP BY CAST(DATE_TRUNC('month', order_date) AS date)
  22.  
  23. --SELECT a.month, a.customers_this_month, c.total_customers, --ROUND(a.customers_this_month*1.0/c.total_customers*100,2)
  24. --FROM a
  25. --LEFT JOIN c ON a.month=c.month
  26. --ORDER BY a.month

Reply to "Задача 35"

Here you can reply to the paste above