简体   繁体   中英

Check if item exists for each partition

Suppose there are 5 shopping bag that need be filled with either 3 or 4 distinct items. It is already decided beforehand what items need to be in each shopping cart.

This info. is portrayed in an excel file in the following way.

Bag Item
1 milk
cheese
eggs
honey
2 ham
chicken
pork
fish
3 bread
mayo
ketchup
4 potato chips
broccoli
mixed greens
5 Jalapeno peppers
ice cream
yogurt

A list of available items in the shop is provided also as a column in an excel file - not all items are available in the shop.

Item
milk
cheese
honey
bread
mayo
ketchup
potato chips
broccoli
mixed greens

I want to return the shopping bags and items that can be completely filled. In the above example, we can see that bags 3 and 4 can be completely filled. How can I do this?

Open to using Python, Pandas & SQl. I did a vlookup in excel which was simple enough, but I have over a million rows and it would be impossible to sort through it all to see which bags are completely filled.

Thanks in advance!

Compared to the bartender problem, think of a "bag with items" as a "drink with ingredients required", and the "shop with items" is like a "bartender with ingredients on hand".

Here's one logical solution, as an example:

CREATE TABLE cms_user (
   user_id        int PRIMARY KEY
 , name           varchar(20)
);

INSERT INTO cms_user (user_id, name)
     VALUES (1,'User1'), (2,'User2'), (3,'User3')
;

CREATE TABLE cms_user_ingredient_rs (
   user_id        int
 , ingredient_id  int
 , PRIMARY KEY (user_id, ingredient_id)
);

INSERT INTO cms_user_ingredient_rs (user_id, ingredient_id)
     VALUES (1,1), (1,2), (1,3), (1,5), (2,2), (2,3), (2,4)
          , (3,1), (3,2), (3,3), (3,5), (3,6)
;

CREATE TABLE cms_drink (
   drink_id       int PRIMARY KEY
 , name           varchar(20)
);

INSERT INTO cms_drink (drink_id, name)
     VALUES (10,'Test'), (15,'Test2')
;

CREATE TABLE cms_drink_ingredient_rs (
   drink_id       int
 , ingredient_id  int
 , PRIMARY KEY (drink_id, ingredient_id)
);

INSERT INTO cms_drink_ingredient_rs (drink_id, ingredient_id)
     VALUES (10,1), (10,3), (10,5), (15,1), (15,6)
;

WITH v2 AS (
         SELECT t1.drink_id      drink_id
              , t1.ingredient_id ingredient_id
              , v1.user_id       user_id
           FROM cms_drink_ingredient_rs AS t1
           CROSS JOIN cms_user AS v1
     )
SELECT v2.user_id, v2.drink_id
  FROM v2
  LEFT JOIN cms_user_ingredient_rs v3
    ON (v2.ingredient_id,v2.user_id) = (v3.ingredient_id,v3.user_id)
 GROUP BY v2.user_id, v2.drink_id
HAVING COUNT(if(v3.user_id IS NULL, 1, null))=0
;

+---------+----------+
| user_id | drink_id |
+---------+----------+
|       1 |       10 |
|       3 |       10 |
|       3 |       15 |
+---------+----------+

Note: There are other solutions to this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM