sql - ERROR: duplicate key value violates unique constraint in postgres -
here slo_order_item_id unique constraint
insert shb.sale_order ( slo_order_item_id, slo_order_id, slo_channel, slo_status, slo_channel_status, slo_order_date, slo_dispatch_by_date, slo_sku, slo_quantity, slo_selling_price, slo_shipping_charge, slo_vendor_id ) select distinct vss_order_item_id, vss_order_id, vss_channel_name, vss_sale_order_item_status, vss_sale_order_item_status, case when is_date(vss_order_date) vss_order_date::date else null end, case when is_date(vss_dispatch_date) vss_dispatch_date::date else null end, vss_sku, 1, vss_selling_price, vss_shipping_charge, vss_vendor_id imp.vendor_sale_staging udt not exists (select 1 shb.sale_order slo_order_item_id = udt.vss_order_item_id);
and try
where vss_order_item_id not in (select slo_order_item_id shb.sale_order);
but these both giving error.
error: duplicate key value violates unique constraint "unique_sale_order_slo_order_item_id" detail: key (slo_order_item_id)=(1027559930) exists.
why giving error ? 1 where condition
work fast , why ?
you may have duplicate values in staging table. can check doing:
select vss_order_item_id, count(*) imp.vendor_sale_staging group vss_order_item_id having count(*) > 1;
if case, suggest fixing staging table. but, fast hack load use distinct on
rather distinct
:
select distinct on (vss_order_item_id) . . . imp.vendor_sale_staging udt not exists (select 1 shb.sale_order slo_order_item_id = udt.vss_order_item_id) order vss_order_item_id;
Comments
Post a Comment