What is my mistake in this statement?
Select Max (TBLvirtual.c2) as MOF --> Most ordered food (MOF)--//
,TBLvirtual.c1
from
(select a.OrdItems as c1, count(a.OrdID) as c2
from Orderrouter a
group by a.OrdItems) as TBLvirtual
I received this error:
Msg 8120, Level 16, State 1, Line 3 Column ‘TBLvirtual.c1’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
The inner Group By for count has already made C1 unique so there is no need doing aggregate on them for MAX. Do this
That will give you the highest count first followed by the next. If you want top 1 or top N, do