Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.67 KB | None | 0 0
  1.  
  2. SET @return_fee = 10;
  3. SET @order_handling_fee = 25;
  4. SELECT
  5.     sku,
  6.     COUNT(order_id) AS total_orders,
  7.     ROUND(AVG(profit), 2) AS avg_profit,
  8.     ROUND(price, 2) AS price,
  9.     ROUND(cost, 2) AS cost,
  10.     SUM(qty_invoiced) AS total_qty_invoiced,
  11.     SUM(qty_refunded) AS total_qty_refunded,
  12.     GROUP_CONCAT(ROUND(t.profit, 2)) AS profits_per_order,
  13.     GROUP_CONCAT(ROUND(order_return_rate)) AS orders_returnrates,
  14.     ROUND(AVG(order_return_rate)) AS avg_order_returnrate,
  15.     CONCAT(ROUND((SUM(qty_refunded) / SUM(qty_invoiced)) * 100),
  16.             '%') AS product_returnrate,
  17.     GROUP_CONCAT(DISTINCT increment_id) AS orders,
  18.     GROUP_CONCAT(DISTINCT bought_together) AS bought_together
  19. FROM
  20.     (SELECT
  21.         main_table.order_id AS order_id,
  22.             orders.increment_id AS increment_id,
  23.             main_table.product_id AS product_id,
  24.             IF(main_table.parent_item_id IS NULL, main_table.sku, parent_item.sku) AS sku,
  25.             main_table.product_type AS product_type,
  26.             main_table.created_at AS created_at,
  27.             main_table.qty_invoiced,
  28.             main_table.qty_refunded,
  29.             order_totals.order_return_rate,
  30.             order_totals.bought_together AS bought_together,
  31.             IF(main_table.parent_item_id IS NULL, main_table.base_cost, parent_item.base_cost) AS cost,
  32.             IF(main_table.parent_item_id IS NULL, main_table.price, parent_item.price) AS price,
  33.             IF(main_table.parent_item_id IS NULL, main_table.price, parent_item.price) * (main_table.qty_invoiced - main_table.qty_refunded) - IF(main_table.parent_item_id IS NULL, main_table.base_cost, parent_item.base_cost) * (main_table.qty_invoiced - main_table.qty_refunded) - (@return_fee / 100) * order_totals.order_return_rate - order_totals.order_fee AS profit
  34.     FROM
  35.         sales_flat_order_item AS main_table
  36.     LEFT JOIN sales_flat_order_item AS parent_item ON parent_item.item_id = main_table.parent_item_id
  37.     INNER JOIN sales_flat_order AS orders ON orders.entity_id = main_table.order_id
  38.     INNER JOIN (SELECT DISTINCT
  39.         order_id AS order_id,
  40.             GROUP_CONCAT(DISTINCT sku) AS bought_together,
  41.             SUM(qty_invoiced) AS order_qty_invoiced,
  42.             SUM(qty_refunded) AS order_qty_refunded,
  43.             (SUM(qty_refunded) / SUM(qty_invoiced)) * 100 AS order_return_rate,
  44.             @order_handling_fee / COUNT(DISTINCT item_id) AS order_fee
  45.     FROM
  46.         sales_flat_order_item
  47.     GROUP BY order_id) AS order_totals ON main_table.order_id = order_totals.order_id
  48.     WHERE
  49.         main_table.product_type = 'simple') AS t
  50. WHERE
  51.     t.created_at > DATE_SUB(NOW(), INTERVAL 1 month)
  52. GROUP BY product_id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement