@Siege I offered two solutions, temp tables and CTE/
with
. In fact, your big query is doing it with a subquery FROM (VALUES ('22rgr45dhs'),('35rg4pcpab'),('37rgtf08ab'),('37RGDF10BS'), ('27MIETE11K')) I([item_or_pricecode])
. I'll explain them better in my answer, but I think you need to go brush up on SQL, temp tables, subqueries, and CTE. SQL does not work like other programming languages you may be familiar with.