Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Select Query with Counts Help

Posted on 2015-02-16
Medium Priority
103 Views
I have 3 tables. They are actually different but I will make up 3 simple tables to show my problem:

Table 1:
FoodType
FoodTypeId
FoodTypeName

1 Fruit
2 Vegetables
3 Meat

Table 2:
Food
FoodId
FoodName
FoodTypeId

1 Apples, 1
2 Pears, 1
3 Potatoes, 2

Table 3:
Eat
EatId
FoodId

I want to select a count of the food types:
There should be 3 from the FoodType table.
I want to return how many FoodTypeIds are in the Eat table by count. The Eat table doesn't contain a FoodtypeId, but does contain the FoodId which has the FoodtypeId.
In my example, the results I need to return are these (but I will actually return the FoodTypeId, not the FoodName):

Fruit = 2
Vegetables = 1
Meat = 0

Any ideas?

I tried this but it is a dismal failure:
``````select count(food.footypeid) as typecount,
food.footypeid
from food
left outer join Eat on food.foodId = Eat.foodId
group by food.foodTypeid
order by count(food.foodTypeid)
``````

thanks!
0
Question by:Starr Duskk
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3

LVL 25

Accepted Solution

chaau earned 2000 total points
ID: 40613257
You need to make sure that the FoodType appears regardless whether it exists in the eat table or not. Therefore it needs to be the first table in the select query and other tables need to be left joined with it:
``````select ft.footypeid,
ft.FoodTypeName,
coalesce(count(Eat.foodId), 0) as typecount
from FoodType ft
left outer join food f ON ft.footypeid = f.footypeid
left outer join Eat on f.foodId = Eat.foodId
group by ft.footypeid,
ft.FoodTypeName,
order by coalesce(count(Eat.foodId), 0) desc
``````
0

LVL 49

Expert Comment

ID: 40613275
if Food.FoodTypeId is always present (i.e. not null) then you could use an inner join at line 5 above

no points pl.
0

LVL 2

Author Comment

ID: 40613278
Perfect perfect perfect!!! thanks so much!
0

LVL 2

Author Closing Comment

ID: 40613279
thank you!  (food type is not always present, so that's why I needed it to return a 0 for meat).

PERFECT!

:)
0

LVL 2

Author Comment

ID: 40613292
oh oh, yes, PortletPaul, yes, the Inner Join is good. Then I won't get ones in FoodType that are NOT In Food. They must be in Food. thanks!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
###### Suggested Courses
Course of the Month11 days, 12 hours left to enroll