I want to change a specific calculation inside a select query to a variable in order to re-process the result for other calculations inside the query. In this example the value I need as variable is dt_raw
. So far I wasn't successful with implementing a solution.
SELECT
cpe.entity_id AS product_id,
cpe.sku,
CASE
WHEN (csi.qty - csi.stock_ordered_qty) >= cped2.value AND csi.stock_id = 3 /* on stock burgsdorgstrasse */
THEN 3
WHEN (csi.qty - csi.stock_ordered_qty) >= cped2.value AND csi.stock_id = 4 /* on stock hoppegarten */
THEN 7
WHEN sum(sm_qty) > 2 AND csi.stock_id = 3 /* history burgsdorfstrasse */
THEN round(avg(DATEDIFF(sm.sm_date, po.po_date)) + 3)
WHEN sum(sm_qty) > 2 AND csi.stock_id = 4 /* history hoppegarten */
THEN round(avg(DATEDIFF(sm.sm_date, po.po_date)) + 4)
ELSE cped.value /* default_delivery_time */
END AS dt_raw
FROM `test_live`.`catalog_product_entity` cpe
LEFT JOIN `test_live`.`cataloginventory_stock_item` csi
ON cpe.entity_id = csi.product_id
LEFT JOIN `test_live`.`catalog_product_entity_decimal` cped /* default_delivery_time */
ON cpe.entity_id = cped.entity_id
AND cped.attribute_id = 392
LEFT JOIN `test_live`.`catalog_product_entity_decimal` cped2 /* min_qty_delivery_time */
ON cpe.entity_id = cped2.entity_id
AND cped2.attribute_id = 393
LEFT JOIN `test_live`.`stock_movement` sm
ON cpe.entity_id = sm.sm_product_id
AND sm.sm_type = "supply"
AND sm.sm_date > NOW() - Interval 90 DAY
LEFT JOIN `test_live`.`purchase_order` po
ON po.po_num = sm.sm_po_num
WHERE
csi.is_favorite_warehouse = 1
AND (csi.stock_id = 3 OR csi.stock_id = 4)
GROUP BY cpe.entity_id
I want to use the results of dt_raw
in further calcuations.
eg:
concat ("ca. ", round(dt_raw), " weeks") as delivery_time
CASE
WHEN dt_raw <= 30
THEN 50
ELSE 0
END AS amazon_qty
If you want to use your query result without restictions you should save it in a table. After that use a select to get the data you want.
CREATE
CREATE TABLE your_schema.dt_raw_temp
SELECT
cpe.entity_id AS product_id,
cpe.sku,
CASE
WHEN (csi.qty - csi.stock_ordered_qty) >= cped2.value AND csi.stock_id = 3 /* on stock burgsdorgstrasse */
THEN 3
WHEN (csi.qty - csi.stock_ordered_qty) >= cped2.value AND csi.stock_id = 4 /* on stock hoppegarten */
THEN 7
WHEN sum(sm_qty) > 2 AND csi.stock_id = 3 /* history burgsdorfstrasse */
THEN round(avg(DATEDIFF(sm.sm_date, po.po_date)) + 3)
WHEN sum(sm_qty) > 2 AND csi.stock_id = 4 /* history hoppegarten */
THEN round(avg(DATEDIFF(sm.sm_date, po.po_date)) + 4)
ELSE cped.value /* default_delivery_time */
END AS dt_raw
FROM `test_live`.`catalog_product_entity` cpe
LEFT JOIN `test_live`.`cataloginventory_stock_item` csi
ON cpe.entity_id = csi.product_id
LEFT JOIN `test_live`.`catalog_product_entity_decimal` cped /* default_delivery_time */
ON cpe.entity_id = cped.entity_id
AND cped.attribute_id = 392
LEFT JOIN `test_live`.`catalog_product_entity_decimal` cped2 /* min_qty_delivery_time */
ON cpe.entity_id = cped2.entity_id
AND cped2.attribute_id = 393
LEFT JOIN `test_live`.`stock_movement` sm
ON cpe.entity_id = sm.sm_product_id
AND sm.sm_type = "supply"
AND sm.sm_date > NOW() - Interval 90 DAY
LEFT JOIN `test_live`.`purchase_order` po
ON po.po_num = sm.sm_po_num
WHERE
csi.is_favorite_warehouse = 1
AND (csi.stock_id = 3 OR csi.stock_id = 4)
GROUP BY cpe.entity_id;
SELECT
SELECT CONCAT('ca. ', round(dt_raw), ' weeks') AS delivery_time,
IF(dt_raw <= 30, 50, 0) AS amazon_qty
FROM your_schema.dt_raw_temp;
The advantage of using tables is that you can reproduce your calculations at any given moment as long as you keep your tables.
Simply select from your query:
select
product_id,
sku,
dt_raw,
concat ("ca. ", round(dt_raw), " weeks") as delivery_time,
case when dt_raw <= 30 then 50 else 0 end as amazon_qty
from
(
SELECT
cpe.entity_id AS product_id,
cpe.sku,
CASE
WHEN (csi.qty - csi.stock_ordered_qty) >= cped2.value AND csi.stock_id = 3 /* on stock burgsdorgstrasse */
THEN 3
WHEN (csi.qty - csi.stock_ordered_qty) >= cped2.value AND csi.stock_id = 4 /* on stock hoppegarten */
THEN 7
WHEN sum(sm.sm_qty) > 2 AND csi.stock_id = 3 /* history burgsdorfstrasse */
THEN round(avg(DATEDIFF(sm.sm_date, po.po_date)) + 3)
WHEN sum(sm.sm_qty) > 2 AND csi.stock_id = 4 /* history hoppegarten */
THEN round(avg(DATEDIFF(sm.sm_date, po.po_date)) + 4)
ELSE cped.value /* default_delivery_time */
END AS dt_raw
FROM test_live.catalog_product_entity cpe
LEFT JOIN test_live.cataloginventory_stock_item csi ON cpe.entity_id = csi.product_id AND csi.is_favorite_warehouse = 1 AND csi.stock_id in (3,4)
LEFT JOIN test_live.catalog_product_entity_decimal cped /* default_delivery_time */ ON cpe.entity_id = cped.entity_id AND cped.attribute_id = 392
LEFT JOIN test_live.catalog_product_entity_decimal cped2 /* min_qty_delivery_time */ ON cpe.entity_id = cped2.entity_id AND cped2.attribute_id = 393
LEFT JOIN test_live.stock_movement sm ON cpe.entity_id = sm.sm_product_id AND sm.sm_type = 'supply' AND sm.sm_date > NOW() - Interval 90 DAY
LEFT JOIN test_live.purchase_order po ON po.po_num = sm.sm_po_num
GROUP BY cpe.entity_id
) prod;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.