I have the following code:
update tble
set
offer_id_02 = offer_id_03,
offer_id_03 = offer_id_04,
offer_id_04 = offer_id_05,
offer_id_05 = offer_id_06,
offer_id_06 = offer_id_07,
offer_id_07 = offer_id_08,
offer_id_08 = NULL
where offer_id_02 = offer_id_01;
update tble
set
offer_id_03 = offer_id_04,
offer_id_04 = offer_id_05,
offer_id_05 = offer_id_06,
offer_id_06 = offer_id_07,
offer_id_07 = offer_id_08,
offer_id_08 = NULL
where offer_id_03 = offer_id_01 or offer_id_03 = offer_id_02;
update tble
set
offer_id_04 = offer_id_05,
offer_id_05 = offer_id_06,
offer_id_06 = offer_id_07,
offer_id_07 = offer_id_08,
offer_id_08 = NULL
where offer_id_04 = offer_id_01 or offer_id_04 = offer_id_02 or offer_id_04 = offer_id_03;
update tble
set
offer_id_05 = offer_id_06,
offer_id_06 = offer_id_07,
offer_id_07 = offer_id_08,
offer_id_08 = NULL
where offer_id_05 = offer_id_01 or offer_id_05 = offer_id_02 or offer_id_05 = offer_id_03 or offer_id_05 = offer_id_04;
update tble
set
offer_id_06 = offer_id_07,
offer_id_07 = offer_id_08,
offer_id_08 = NULL
where offer_id_06 = offer_id_01 or offer_id_06 = offer_id_02 or offer_id_06 = offer_id_03 or offer_id_06 = offer_id_04 or offer_id_06 = offer_id_05;
update tble
set
offer_id_07 = offer_id_08,
offer_id_08 = NULL
where offer_id_07 = offer_id_01 or offer_id_07 = offer_id_02 or offer_id_07 = offer_id_03 or offer_id_07 = offer_id_04 or offer_id_07 = offer_id_05 or offer_id_07 = offer_id_06;
update tble
set
offer_id_08 = NULL
where offer_id_08 = offer_id_01 or offer_id_08 = offer_id_02 or offer_id_08 = offer_id_03 or offer_id_08 = offer_id_04 or offer_id_08 = offer_id_05 or offer_id_08 = offer_id_06 or offer_id_08 = offer_id_07;
Essentially, I have a SQL table with a customer and 8 offers they get as a promotion. Due to bad logic, some duplicates come through. To fix this I "scoot over" the duplicate offer and null out offer_id_08 slot. The issue is, the logic is different depending on which offers are duplicate to each other. How can I build this out in a concise way?
Maybe, as a first step, you could try to merge your queries into one big query, using CASE's and combined WHERE's: