2013-04-12 3 views
3

Привет У меня есть эта таблица,SQL Query подсчитывать

Recipe = (idR, recipeTitle, prepText, cuisineType, mealType) 
    Ingredient = (idI, ingrDesc) 
    RecipIngr = (idR*, idI*) 

и я пытаюсь запросить список для ingrDesc с отсчетом сколько рецептах, что ingrDesc находится. Я хочу перечислить только те ingrDesc которые происходят более 10 раз.

Вот что у меня есть:

SELECT a.idI, a.recipeTitle 
    FROM Recipe a 
    INNER JOIN recpingr b 
    ON a.idr = b.idr 
    WHERE a.preptext = '>10' 

Любая помощь, как я не знаю, как вести с этим запросом

+0

см [имея] (http://en.wikipedia.org/wiki/Having_%28SQL%29). – Aprillion

ответ

3

Использование GROUP BY с HAVING:

SELECT i.idI, i.ingrDesc, COUNT(*) 
FROM Ingredient i 
INNER JOIN RecipIngr ri ON i.idI = ri.idI 
GROUP BY i.idI, i.ingrDesc 
HAVING COUNT(*) > 10 
+0

Это просто работает. идеально. – user182

1

Вам нужно использовать групповое предложение и иметь. Я создал быстрый образец здесь, но мои данные образца не доходят до 10, поэтому я использовал любой ингредиент, который использовался более одного раза (> 1).

Вот образец данных:

create table dbo.recipe (
     idR    int    not null, 
     recipeTitle  varchar(100) not null, 
     prepText  varchar(4000) null, 
     cuisineType  varchar(100) null, 
     mealType  varchar(100) null 
    ) 
    go 

    insert into dbo.recipe values (1, 'Eggs and Bacon', 'Prep Text 1', 'American', 'Breakfast') 
    insert into dbo.recipe values (2, 'Turkey Sandwich', 'Prep Text 2', 'American', 'Lunch') 
    insert into dbo.recipe values (3, 'Roast Beef Sandwich', 'Prep Text 3', 'American', 'Lunch') 
    go 

    create table dbo.ingredient (
     idI    int    not null, 
     ingrDesc  varchar(200) not null 
    ) 
    go 

    insert into dbo.ingredient values (1, 'Large Egg') 
    insert into dbo.ingredient values (2, 'Bacon'); 
    insert into dbo.ingredient values (3, 'Butter'); 
    insert into dbo.ingredient values (4, 'Sliced Turkey'); 
    insert into dbo.ingredient values (5, 'Lettuce'); 
    insert into dbo.ingredient values (6, 'Tomato'); 
    insert into dbo.ingredient values (7, 'Onion'); 
    insert into dbo.ingredient values (8, 'Bread'); 
    insert into dbo.ingredient values (9, 'Mustard'); 
    insert into dbo.ingredient values (10, 'Horseradish'); 
    insert into dbo.ingredient values (11, 'Sliced Roast Beef'); 
    go 

    create table dbo.recipingr(
     idR    int    not null, 
     idI    int    not null 
    ) 
    go 

    insert into dbo.recipingr values (1, 1); 
    insert into dbo.recipingr values (1, 2); 
    insert into dbo.recipingr values (2, 4); 
    insert into dbo.recipingr values (2, 5); 
    insert into dbo.recipingr values (2, 6); 
    insert into dbo.recipingr values (2, 7); 
    insert into dbo.recipingr values (2, 8); 
    insert into dbo.recipingr values (2, 9); 
    insert into dbo.recipingr values (3, 11); 
    insert into dbo.recipingr values (3, 10); 
    insert into dbo.recipingr values (3, 8); 
    insert into dbo.recipingr values (3, 6); 
    insert into dbo.recipingr values (3, 5); 
    go 

Вот запрос:

select 
     i.ingrDesc, 
     count(*) ingrCount 
    from 
     dbo.recipe r 
     inner join dbo.recipingr ri on ri.idR = r.idR 
     inner join dbo.ingredient i on i.idI = ri.idI 
    group by 
     i.ingrDesc 
    having 
     count(*) > 1 
+0

Оцените усилия для этого! Благодарю. – user182

Смежные вопросы