35 lines
1.3 KiB
SQL
35 lines
1.3 KiB
SQL
-- Step 3: 填充 fact_inventory_snapshot 从盘点倒挤数据ETL
|
|
-- 需要将 cost_unit_code 映射为 store_code,盘点数据中有 sales_store_code 可用
|
|
|
|
INSERT INTO analytics.fact_inventory_snapshot (
|
|
store_code, material_code, snapshot_date,
|
|
opening_quantity, opening_amount,
|
|
purchase_quantity, purchase_amount,
|
|
consumption_quantity, consumption_amount,
|
|
ending_quantity, ending_amount,
|
|
waste_quantity, waste_amount,
|
|
transfer_in_quantity, transfer_out_quantity,
|
|
inventory_days, is_negative, created_at
|
|
)
|
|
SELECT
|
|
COALESCE(s.sales_store_code, i.cost_unit_code) AS store_code,
|
|
i.item_code AS material_code,
|
|
i.report_month AS snapshot_date,
|
|
i.opening_quantity, i.opening_amount,
|
|
i.purchase_quantity, i.purchase_amount,
|
|
i.consumption_quantity,
|
|
i.consumption_amount,
|
|
i.ending_quantity, i.ending_amount,
|
|
NULL::numeric AS waste_quantity,
|
|
COALESCE(i.return_loss_amount, 0) AS waste_amount,
|
|
NULL::numeric AS transfer_in_quantity,
|
|
NULL::numeric AS transfer_out_quantity,
|
|
NULL::numeric AS inventory_days,
|
|
i.is_negative_consumption AS is_negative,
|
|
now() AS created_at
|
|
FROM inventory_cost_records i
|
|
LEFT JOIN inventory_store_mapping s ON i.cost_unit_code = s.cost_unit_code
|
|
WHERE i.report_month = DATE '2026-04-01'
|
|
AND i.item_code IS NOT NULL AND i.item_code != ''
|
|
ON CONFLICT DO NOTHING;
|