简体   繁体   中英

SQL query to find missing data between 2 tables with groups

I've tried a few ways to achieve what's next but there's got to be an easy way.

Assuming I have cooking recipes. Each recipe has ingredients. When workers prepares a recipe, they do so in multiples batches.

I'm trying to figure out how to find the batches that are missing which ingredient to insert into another table later on, which has all the preparation data. Currently, it only shows the data for ingredients that were actually used.

Here's the data:

CREATE TABLE #Repipe 
(
    Recipe VARCHAR(1)
    , Ingredient VARCHAR(2)
)

INSERT INTO #Repipe (Recipe, Ingredient)
VALUES
(1, 1)
, (1, 2)
, (1, 3)
, (1, 4)

CREATE TABLE #RecipePreparation
(
    Recipe VARCHAR(1)
    , Batch SMALLINT
    , Ingredient VARCHAR(2)
)

INSERT INTO #RecipePreparation (Recipe, Batch, Ingredient)
VALUES
(1, 1, 1)
, (1, 1, 2)
, (1, 1, 3)
, (1, 1, 4)
, (1, 2, 1)
, (1, 2, 2)
, (1, 2, 3)
, (1, 2, 4)
, (1, 3, 1)
, (1, 3, 3)
, (1, 3, 4)

DROP TABLE #RecipePreparation
DROP TABLE #Repipe

As you can see, the batch number 3 is missing ingredient #2.

If I understand correctly, you basically want to get all the records from the #Recipe table that do not have a corresponding (Recipe, Ingredient) record in #RecipePreparation ?

Something like this should accomplish what you need, if I understand your problem correctly. Query is not tested.

SELECT *
FROM (SELECT DISTINCT Recipe, Batch FROM #RecipePreparation) xrp
    LEFT JOIN #Recipe r on r.Recipe = xrp.Recipe
    LEFT JOIN #RecipePreparation rp on rp.Recipe = xrp.Recipe AND rp.Batch = xrp.Batch AND rp.Ingredient = r.Ingredient
WHERE rp.Ingredient IS NULL

Is the data model fixed or are you still developing, because what you would ideally want is something like this:

在此处输入图像描述

If things are fixed, then the solution provided by Ruslan will suffice (the SELECT DISTINCT is to overcome the fact that the Batch entity is not explicit in the current model).

Motivation for the changes is to make sure data issues do not arise and so the query engine can properly optimize.

It also makes answering your question a little more direct:

SELECT
  Batch.Recipe
 ,Batch.Batch
 ,RecipeIngredient.Ingredient
FROM
  Batch Batch
INNER JOIN
  RecipeIngredient RecipeIngredient
    ON RecipeIngredient.Recipe = Batch.Recipe

  EXCEPT

SELECT
  Recipe
 ,Batch
 ,Ingredient
FROM
  BatchIngredient

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