I’ve got a query like this:
SELECT * FROM RecipeTable, RecipeIngredientTable, SyncRecipeIngredientTable WHERE RecipeTable.recipe_id = SyncRecipeIngredientTable.recipe_id AND RecipeIngredientTable.recipe_ingredient_id = SyncRecipeIngredientTable.recipe_ingredient_id AND RecipeIngredientTable.recipe_item_name in ("ayva", "pirinç", "su") GROUP by RecipeTable.recipe_id HAVING COUNT(*) >= 3;
and this query returns the result like this:
As you can see in the image there is 3 duplicate, unnecessary entries (no, i can’t delete them because of the multiple foreign keys). How can I deselect these duplicate entries from the result query? In the end I want to return 6 entries not 9.
Advertisement
Answer
What you want to eliminate in the result set is not duplication of recipe_id
values but recipe_name
values.
You just need to group(partition) by recipe_name through use of ROW_NUMBER()
analytic function :
SELECT recipe_id, author_name ... FROM ( SELECT ROW_NUMBER() OVER (PARTITION BY recipe_name) AS rn, sr.recipe_id, author_name ... FROM SyncRecipeIngredientTable sr JOIN RecipeIngredientTable ri ON ri.recipe_ingredient_id = sr.recipe_ingredient_id JOIN RecipeTable rt ON rt.recipe_id = sr.recipe_id WHERE ri.recipe_item_name in ("ayva", "pirinç", "su") ) WHERE rn = 1
This way, you can pick only one of the records with rn=1
(ORDER BY
Clause might be added to that analytic function after PARTITION BY
clause if spesific record is needed to be picked)