sql

mail@pastecode.io avatar
unknown
sql
2 years ago
2.3 kB
2
Indexable
Never
SELECT "internal_api_warehouse"."id",
       "internal_api_warehouse"."product_unit_id",
       "internal_api_warehouse"."shop_id",
       "internal_api_warehouse"."min_remaining",
       "internal_api_warehouse"."max_remaining",
       "internal_api_warehouse"."supplier_id",
       "internal_api_warehouse"."margin",
       "internal_api_warehouse"."auto_order",
       "internal_api_warehouse"."price",
       ((("internal_api_warehouse"."margin" + 100) * MAX("internal_api_warehouserecord"."cost") FILTER (WHERE "internal_api_warehouserecord"."quantity" > 0)) / 100) AS "recommended_price",
       COALESCE(SUM("internal_api_warehouserecord"."quantity"), 0) AS "remaining",
       "products_productunit"."barcode" AS "barcode",
       "products_productunit"."weight" AS "weight",
       "products_productunit"."for_weighing_scales" AS "for_weighing_scales",
       "products_productunit"."for_scales" AS "for_scales",
       "products_product"."vat_rate" AS "vat_rate",
       "products_product"."category_id" AS "category_id",
       "products_category"."name" AS "category_name",
       "products_measurementunit"."name" AS "unit_name",
       "products_productunit"."unit_id" AS "unit_id"
  FROM "internal_api_warehouse"
  LEFT OUTER JOIN "internal_api_warehouserecord"
    ON ("internal_api_warehouse"."id" = "internal_api_warehouserecord"."warehouse_id")
 INNER JOIN "products_productunit"
    ON ("internal_api_warehouse"."product_unit_id" = "products_productunit"."id")
 INNER JOIN "products_product"
    ON ("products_productunit"."product_id" = "products_product"."id")
  LEFT OUTER JOIN "products_category"
    ON ("products_product"."category_id" = "products_category"."id")
 INNER JOIN "products_measurementunit"
    ON ("products_productunit"."unit_id" = "products_measurementunit"."id")
 WHERE "internal_api_warehouse"."shop_id" = 3
 GROUP BY "internal_api_warehouse"."id", ("internal_api_warehouse"."margin" + 100), "products_productunit"."barcode",
       "products_productunit"."weight",
       "products_productunit"."for_weighing_scales",
       "products_productunit"."for_scales",
       "products_product"."vat_rate",
       "products_product"."category_id",
       "products_category"."name",
       "products_measurementunit"."name",
       "products_productunit"."unit_id"