Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to fetch food name from food table order by most inserted food name.
Posted
Comments
Azee 7-Oct-13 2:27am    
So you mean, that there are multiple entries in table of a specific food name?
prthghosh999 7-Oct-13 2:56am    
yes..
Thanks7872 7-Oct-13 2:31am    
Why you expect this much from others? Read this question twice and think of it. Is this a question? How some one can assume everything?

Here try something like this, Since I don't exactly know the column and table names, So here it goes:

SQL
SELECT FoodName, COUNT(FoodName) FROM dbo.FoodTable GROUP BY FoodName
ORDER BY COUNT(FoodName) DESC


Let me know, if you expected something different.

Azee...
 
Share this answer
 
Comments
Mehdi Gholam 7-Oct-13 2:41am    
Just posted the same, 5'ed
prthghosh999 7-Oct-13 3:13am    
it works for me. many many thanks to you..
i also tried this code but my fault was i use "select * from ...."
and it gives me an error
prthghosh999 7-Oct-13 3:15am    
because of food_Id..
Generally to write such queries you should break down what you are saying, so:

"...inserted food name" (get the count of food name to see which is most inserted):
SQL
select foodname, count(foodname) as c from foodtable group by foodname

"fetch food name order by most inserted..." (uses the above in a compound query):
SQL
select t.foodname from 
(  select foodname, count(foodname) as c from foodtable group by foodname ) 
as t order by t.c desc

Now you can optimize this, but it's a good place to start.
 
Share this answer
 

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