-- liquibase formatted sql
-- changeset Enson:fill_stocktakerecord_lastSelect

UPDATE `fpsmsdb`.`stocktakerecord`
SET lastSelect = CASE
    WHEN approverStockTakeQty = pickerSecondStockTakeQty
         AND COALESCE(approverBadQty, 0) = COALESCE(pickerSecondBadQty, 0) THEN 2
    WHEN approverStockTakeQty = pickerFirstStockTakeQty
         AND COALESCE(approverBadQty, 0) = COALESCE(pickerFirstBadQty, 0) THEN 1
    WHEN approverStockTakeQty IS NOT NULL THEN 3
    ELSE lastSelect
END
WHERE lastSelect IS NULL;

