-- P5-3973 Delete incorrect order sum values

delete from "order-orders_fields"
where fieldid = 246 and orderid in (
    select
        orders.orderid
    from
        "order-orders" orders
        join "order-orders_fields" credit_invoice on orders.orderid = credit_invoice.orderid and credit_invoice.fieldid = 199
        join "order-orders_fields" order_sum on orders.orderid = order_sum.orderid and order_sum.fieldid = 246
        left join "order-orders_fields" cancels_orderid on orders.orderid = cancels_orderid.orderid and cancels_orderid.fieldid = 237
        left join "order-orders_fields" order_sum_canceled_order on cancels_orderid.value::int = order_sum_canceled_order.orderid and order_sum_canceled_order.fieldid = 246
    where
        order_sum.value::float > 0 and order_sum_canceled_order.value = order_sum.value
);

