Solved

Select Query with Counts Help

Posted on 2015-02-16
5
92 Views
Last Modified: 2015-02-16
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) 

Open in new window


thanks!
0
Comment
Question by:BobCSD
  • 3
5 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 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 

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
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 1

Author Comment

by:BobCSD
ID: 40613278
Perfect perfect perfect!!! thanks so much!
0
 
LVL 1

Author Closing Comment

by:BobCSD
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 1

Author Comment

by:BobCSD
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now