# noinspection SqlWithoutWhereForFile

UPDATE `storage`
SET
    `area` =
        (
            SELECT spv.valueInteger / 100
            FROM storage_property_value spv
            LEFT JOIN storage_property sp ON sp.id = spv.storagePropertyId
            WHERE sp.codeName = 'width' AND spv.storageId = `storage`.id
        ) *
        (
            SELECT spv.valueInteger / 100
            FROM storage_property_value spv
            LEFT JOIN storage_property sp ON sp.id = spv.storagePropertyId
            WHERE sp.codeName = 'length' AND spv.storageId = `storage`.id
        ),
    `volume` =
        (
            SELECT spv.valueInteger / 100
            FROM storage_property_value spv
            LEFT JOIN storage_property sp ON sp.id = spv.storagePropertyId
            WHERE sp.codeName = 'width' AND spv.storageId = `storage`.id
        ) *
        (
            SELECT spv.valueInteger / 100
            FROM storage_property_value spv
            LEFT JOIN storage_property sp ON sp.id = spv.storagePropertyId
            WHERE sp.codeName = 'length' AND spv.storageId = `storage`.id
        ) *
        (
            SELECT spv.valueInteger / 100
            FROM storage_property_value spv
            LEFT JOIN storage_property sp ON sp.id = spv.storagePropertyId
            WHERE sp.codeName = 'height' AND spv.storageId = `storage`.id
        );

DELETE FROM location WHERE ( SELECT count(*) FROM `storage` WHERE locationId = location.id ) < 1;

DELETE FROM storage_property_value WHERE storageId IN ( SELECT id FROM `storage` WHERE locationId = 0 );
DELETE FROM `storage` WHERE locationId = 0;