Click here to Skip to main content
15,891,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have the following table

RECIPE NAME INGREDIENT
GOBIMANCHURI ONION
GOBIMANCHURI ONION
GOBIMANCHURI OIL
POORI POTATO
POORI POTATO
POORI WHEAT
PONGAL RICE

I WANT TO WRITE A QUERY SO THAT IT SHOULD DISPLAY ALL THE RECIPE NAME FOR WHICH THE SAME INGREDIENT HAS REPEATED MORE THAN ONCE. IN MY ABOVE EXAMPLE I WANT TO DISPLAY GOBIMANCHURI AS WELL AS POORI ALONG WITH THE INGREDIENT NAME. HOW CAN I DO THIS?
Posted
Comments
Maciej Los 27-Nov-13 7:29am    
Do not SHOUT!!! Using only capitals is treated as a SHOUT!

Group by name and ingredient and then filter on count.

Something like this might do the trick for you:
SQL
select recipe_name, ingredient from
(
    select recipe_name, ingredient, count(*) as [count] from recipe group by recipe_name, ingredient
) as source
where [count] > 1

Hope this helps,
Fredrik
 
Share this answer
 
Use DISTINCT clause, which specifies that only unique rows can appear in the result set.

If you would like to count recipes, please try this:
SQL
SELECT RecipeName, Ingredient, Count(RecipeName) AS CountOfRecipes
FROM RecipeTable
GROUP BY RecipeName, Ingredient
 
Share this answer
 
Comments
kparun86 28-Nov-13 2:08am    
Thanks Solution 2 is working.
Maciej Los 28-Nov-13 2:13am    
Thank you ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900