Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 6.19 KB | None | 0 0
  1. WITH "count_shp_packs" AS (SELECT "shp_pack_id", COUNT(*) AS COUNT
  2.                            FROM "ord_items_metadata"
  3.                            WHERE "free_fill_used" = TRUE
  4.                              AND created_at >= now() - INTERVAL '30 days'
  5.                            GROUP BY "shp_pack_id")
  6. SELECT DISTINCT "product_id", COUNT(*)
  7. FROM "prd_versions"
  8.        JOIN shp_packs USING (prd_ver_id)
  9.        LEFT JOIN count_shp_packs ON count_shp_packs.shp_pack_id = shp_packs.shp_pack_id
  10. WHERE "org_warehouse_id" IN (SELECT "ms"."org_warehouse_id"
  11.                              FROM (WITH "activation_spent_warehouses" AS (SELECT "org_warehouse_id",
  12.                                                                                  SUM(oac.activation_cost)    AS sum_activation_cost,
  13.                                                                                  COUNT(ord_item_metadata_id) AS count_ord,
  14.                                                                                  SUM(org_shp_cons_rate)      AS sum_shp_cons_rate
  15.                                                                           FROM "ord_items_metadata" AS "om",
  16.                                                                                "org_warehouse_activation_cost" AS "oac"
  17.                                                                           WHERE "om"."free_fill_used" = TRUE
  18.                                                                             AND om.bill_weight BETWEEN oac.from_billable_weight AND oac.to_billable_weight
  19.                                                                             AND om.created_at >= now() - INTERVAL '30 days'
  20.                                                                           GROUP BY "om"."org_warehouse_id") SELECT "org_warehouse_settings"."org_warehouse_id",
  21.                                                                                                                    json_extract_path_text(
  22.                                                                                                                        org_warehouse_settings.settings::json,
  23.                                                                                                                        'activation_spent_limit_per_month')::NUMERIC -
  24.                                                                                                                    activation_spent_warehouses.sum_activation_cost AS differ_smc,
  25.                                                                                                                    json_extract_path_text(
  26.                                                                                                                        org_warehouse_settings.settings::json,
  27.                                                                                                                        'max_items_count_per_month')::NUMERIC -
  28.                                                                                                                    activation_spent_warehouses.count_ord           AS differ_cr,
  29.                                                                                                                    json_extract_path_text(
  30.                                                                                                                        org_warehouse_settings.settings::json,
  31.                                                                                                                        'max_shipping_limit_value')::NUMERIC *
  32.                                                                                                                    0.1 -
  33.                                                                                                                    activation_spent_warehouses.sum_shp_cons_rate   AS differ_sr
  34.                                                                                                             FROM "org_warehouse_settings"
  35.                                                                                                                    LEFT JOIN activation_spent_warehouses
  36.                                                                                                                              ON activation_spent_warehouses.org_warehouse_id =
  37.                                                                                                                                 org_warehouse_settings.org_warehouse_id
  38.                                                                                                             WHERE
  39.                                                                                                                 "org_warehouse_settings"."org_warehouse_id" IN
  40.                                                                                                                 (SELECT "org_warehouse_id"
  41.                                                                                                                  FROM "org_warehouse_ord_promotion_con"
  42.                                                                                                                  WHERE
  43.                                                                                                                      "ord_promotion_id" IN
  44.                                                                                                                      (SELECT "ord_promotion_id"
  45.                                                                                                                       FROM "ord_promotions"
  46.                                                                                                                       WHERE "code" = 'free_fill'))) AS "ms"
  47.                              WHERE differ_smc IS NULL
  48.                                 OR differ_smc > 0 AND differ_cr IS NULL
  49.                                 OR differ_cr > 0 AND differ_sr IS NULL
  50.                                 OR differ_sr > 0)
  51.   AND "end_at" IS NULL
  52.   AND "product_not_available" = FALSE
  53.   AND "product_published" = TRUE
  54.   AND "shp_packs"."free_fill" = TRUE
  55.   AND ((free_fill_quota - count_shp_packs.COUNT > 0) OR (free_fill_quota > 0 AND count_shp_packs.COUNT IS NULL))
  56. GROUP BY "product_id"
  57. ORDER BY "count" DESC
  58. LIMIT 100
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement