create or replace view vBluesealMarketplaceCategoryInverted as
  SELECT
    `m`.`id`                      AS `marketplaceId`,
    `ma`.`id`                     AS `marketplaceAccountId`,
    `mac`.`marketplaceCategoryId` AS `marketplaceCategoryId`,
    `m`.`name`                    AS `marketplace`,
    `ma`.`name`                   AS `marketplaceAccount`,
    `mac`.`name`                  AS `marketplaceAccountCategory`,
    `mac`.`path`                  AS `marketplaceAccountPath`,
    `pc`.`slug`                   AS `internalCategory`,
    pc.id as productCategoryId,
    pc.slug as productCategory
  FROM
    `ProductCategory` `pc`
    LEFT JOIN `MarketplaceAccount` `ma` on 1=1
    LEFT JOIN `Marketplace` `m` on `m`.`id` = `ma`.`marketplaceId`
    LEFT JOIN `ProductCategoryHasMarketplaceAccountCategory` `pchmac` ON `pc`.`id` = `pchmac`.`productCategoryId` and
                                                                         ma.id = pchmac.marketplaceAccountId AND
                                                                         ma.marketplaceId = pchmac.marketplaceId
    LEFT JOIN `MarketplaceAccountCategory` `mac` ON (`mac`.`marketplaceAccountId` = `pchmac`.`marketplaceAccountId`) AND
                                                    (`mac`.`marketplaceId` = `pchmac`.`marketplaceId`) AND
                                                    (`mac`.`marketplaceCategoryId` = `pchmac`.`marketplaceAccountCategoryId`)
    where pc.id != 1
  group by pc.id, ma.id, m.id;
