Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While Martin Smith's solution does not entirely solve this problem, it did inspire me to look into using CTEs and I think I got it.</p> <p>Once I realized every 3 ingredient combination is really 2 different 2 ingredient combinations that share 1 common ingredient, I decided to find all 2 ingredient combinations and then find every combination of those that have at least 1 ingredient in common and both have effects that the other does not have.</p> <p>Then check to make sure every 3 ingredient combination has no poison effects (I already know each individual 2 ingredient combination has no poison effects, but just because A+B has no poison and B+C has no poison doesn't mean A+B+C won't have poison. It's possible that combing A with C will produce a poison effect).</p> <p>Then I join all 3 ingredients back to the Effect table to display which effects are produced with each combination.</p> <p>This query has a 3 minute 50 second execution time on my system. That's not cool. But at least I am getting the results I want now.</p> <pre><code>WITH Combination AS ( --Finds all 2 ingredient combinations that have shared effects that are not poisons select ROW_NUMBER() OVER (ORDER BY i1.Name, i2.Name) UniqCombination, i1.UniqIngredient UniqIngredient1, i2.UniqIngredient UniqIngredient2, COUNT(1) NumberOfEffects from Ingredient i1 cross join Ingredient i2 INNER JOIN IngredientEffectJT jt1 ON i1.UniqIngredient = jt1.UniqIngredient INNER JOIN IngredientEffectJT jt2 ON i2.UniqIngredient = jt2.UniqIngredient INNER JOIN Effect e ON jt1.UniqEffect = e.UniqEffect AND jt2.UniqEffect = e.UniqEffect WHERE i1.UniqIngredient &lt; i2.UniqIngredient GROUP BY i1.UniqIngredient, i1.name, i2.UniqIngredient, i2.Name HAVING SUM(e.poison) = 0 ), Potion AS ( --Matches up all 2 ingredient combinations in the Combination CTE with the effects for that combination SELECT DISTINCT c.UniqCombination, c.UniqIngredient1, i1.Name Ingredient1, c.UniqIngredient2, i2.Name Ingredient2, e.UniqEffect, e.Name Effect FROM Combination c INNER JOIN Ingredient i1 ON c.UniqIngredient1 = i1.UniqIngredient INNER JOIN Ingredient i2 ON c.UniqIngredient2 = i2.UniqIngredient INNER JOIN IngredientEffectJT jt1 ON c.UniqIngredient1 = jt1.UniqIngredient INNER JOIN IngredientEffectJT jt2 ON c.UniqIngredient2 = jt2.UniqIngredient INNER JOIN Effect e ON jt1.UniqEffect = e.UniqEffect AND jt2.UniqEffect = e.UniqEffect ), BigCombination AS ( --Matches 2 combinations together where 1 ingredient is the same in both combinations. SELECT c1.UniqIngredient1, CASE WHEN c1.UniqIngredient1 = c2.UniqIngredient1 THEN c1.UniqIngredient2 ELSE c2.UniqIngredient1 END UniqIngredient2, c2.UniqIngredient2 UniqIngredient3 FROM Combination c1 INNER JOIN Combination c2 ON (c1.UniqIngredient1 = c2.UniqIngredient1 OR c1.UniqIngredient2 = c2.UniqIngredient1 OR c1.UniqIngredient2 = c2.UniqIngredient2) AND c1.UniqCombination &lt; c2.UniqCombination --This WHERE clause sucks because there are 2 different select queries that must run twice each. --They have to run twice because I have to EXCEPT 1 from 2 and 2 from 1 to make sure both combinations are contributing something new. WHERE EXISTS( SELECT p1.UniqEffect FROM Potion p1 WHERE p1.UniqCombination = c1.UniqCombination EXCEPT SELECT p2.UniqEffect FROM Potion p2 WHERE p2.UniqCombination = c2.UniqCombination) AND EXISTS( SELECT p2.UniqEffect FROM Potion p2 WHERE p2.UniqCombination = c2.UniqCombination EXCEPT SELECT p1.UniqEffect FROM Potion p1 WHERE p1.UniqCombination = c1.UniqCombination) ), BigPotionCombination AS ( --Combinations were made only from other combinations that made potions, but it's possible the new --ingredients mixing together could create a new poison effect. This will remove combinations that create new poison effects SELECT DISTINCT c.* FROM BigCombination c INNER JOIN IngredientEffectJT jt1 ON c.UniqIngredient1 = jt1.UniqIngredient INNER JOIN IngredientEffectJT jt2 ON c.UniqIngredient2 = jt2.UniqIngredient INNER JOIN IngredientEffectJT jt3 ON c.UniqIngredient3 = jt3.UniqIngredient INNER JOIN Effect e ON (jt1.UniqEffect = e.UniqEffect AND (jt2.UniqEffect = e.UniqEffect OR jt3.UniqEffect = e.UniqEffect)) OR (jt2.UniqEffect = e.UniqEffect AND jt3.UniqEffect = e.UniqEffect) GROUP BY c.UniqIngredient1, c.UniqIngredient2, c.UniqIngredient3 HAVING SUM(e.Poison) = 0 ) --Combinations have to be joined back to Effect again to display the effects that the potions have. SELECT DISTINCT i1.Name Ingredient1, i2.Name Ingredient2, i3.Name Ingredient3, e.Name Effect FROM BigPotionCombination c INNER JOIN Ingredient i1 ON c.UniqIngredient1 = i1.UniqIngredient INNER JOIN Ingredient i2 ON c.UniqIngredient2 = i2.UniqIngredient INNER JOIN Ingredient i3 ON c.UniqIngredient3 = i3.UniqIngredient INNER JOIN IngredientEffectJT jt1 ON c.UniqIngredient1 = jt1.UniqIngredient INNER JOIN IngredientEffectJT jt2 ON c.UniqIngredient2 = jt2.UniqIngredient INNER JOIN IngredientEffectJT jt3 ON c.UniqIngredient3 = jt3.UniqIngredient INNER JOIN Effect e ON (jt1.UniqEffect = e.UniqEffect AND (jt2.UniqEffect = e.UniqEffect OR jt3.UniqEffect = e.UniqEffect)) OR (jt2.UniqEffect = e.UniqEffect AND jt3.UniqEffect = e.UniqEffect) ORDER BY Ingredient1, Ingredient2, Ingredient3, Effect </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload