Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.54 KB | None | 0 0
  1. SET @date_from = '2019-04-15',
  2.      @date_to = '2019-04-19';
  3.  
  4. WITH segment_customers
  5. AS (
  6.         SELECT  CONCAT(abt.ab_test_id, '_', abt.ab_test_part) AS segment
  7.                     , IF(LEFT(abt.user_agent,8) = 'MamsyApp'
  8.                         , 'MamsyApp'
  9.                         , IF(abt.is_mobile = 1, 'Mobile', 'Desktop')
  10.                         ) AS platform
  11.                     , ctabt.customers_id
  12.                     , cs.customers_status_name AS customers_status
  13.         FROM ab_testing AS abt
  14.         JOIN customers_to_ab_testing AS ctabt ON abt.session_id = ctabt.ab_testing_session_id
  15.         JOIN customers AS c ON ctabt.customers_id = c.customers_id
  16.         JOIN customers_status AS cs ON c.customers_status = cs.customers_status_id
  17.         WHERE abt.ab_test_id = 4
  18.     )
  19.  
  20. , segment_total
  21. AS (
  22.         SELECT  sc.segment
  23.                     , sc.platform
  24.                     , sc.customers_status
  25.                     , opn.products_categories_id AS categories_id
  26.                     , ce.categories_hide_on_main
  27.                     , COUNT(DISTINCT o.customers_id) AS count_customers
  28.                     , COUNT(DISTINCT o.orders_id) AS count_orders
  29.                     , SUM(GREATEST(1 - opn.is_deleted,0) * opn.products_price) AS orders_amount
  30.         FROM segment_customers AS sc
  31.         JOIN orders AS o ON sc.customers_id = o.customers_id  -- AND o.date_purchased BETWEEN @date_from AND @date_to
  32.         JOIN orders_products_new AS opn ON o.orders_id = opn.orders_id AND opn.op_date_added BETWEEN @date_from AND @date_to
  33.         /*LEFT*/ JOIN categories_extra AS ce ON opn.products_categories_id = ce.categories_id
  34.         GROUP BY    sc.segment
  35.                     , sc.platform
  36.                     , sc.customers_status
  37.                     , opn.products_categories_id
  38.                     , ce.categories_hide_on_main
  39.         )
  40.  
  41. SELECT *
  42. FROM segment_total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement