-- cleaning of all data from new table of blacklisting products
TRUNCATE TABLE "product_blacklist";

INSERT INTO product_blacklist (product_id, group_id, language_id, territory_id)

  -- getting all blacklisted designers' canvas products
  SELECT  ppc.productid,
    2 as groupid,
    pcb.language,
    pcb.territory
  FROM "product-categories_blacklist" pcb
  INNER JOIN "designers" d ON pcb.categoryid = d.categoryid
  INNER JOIN "product-products_categories" ppc ON pcb.categoryid = ppc.categoryid
  WHERE pcb.productgroup = 'canvas' OR pcb.productgroup IS NULL
  GROUP BY ppc.productid, groupid, pcb.language, pcb.territory

  UNION

  -- getting all blacklisted designers' wallpaper products
  SELECT  ppc.productid,
    groupid,
    pcb.language,
    pcb.territory
  FROM "product-categories_blacklist" pcb
  CROSS JOIN (SELECT groupid from "product-groups" where groupid = 1 OR groupid = 3) as printproducts
  INNER JOIN "designers" d ON pcb.categoryid = d.categoryid
  INNER JOIN "product-products_categories" ppc ON pcb.categoryid = ppc.categoryid
  INNER JOIN "product-products" products ON ppc.productid = products.productid
  WHERE pcb.productgroup = 'photo-wallpaper' OR pcb.productgroup IS NULL
  GROUP BY ppc.productid, groupid, pcb.language, pcb.territory
  ORDER BY productid, territory, language, groupid;