I'm getting this error:
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified
From this query:
(SELECT Z.dMonth,Z.dCount FROM (
SELECT COUNT(*) dCount, month(tblDiving.date_of_diving) as dMonth
FROM tblDiving
GROUP BY month(tblDiving.date_of_diving)
)Z
ORDER BY Z.dCount)
I understand I need to add "Top" Somewhere in this query, but where? In the end, this query need to return the Maximum dCount and the month of this dCount.
Edited:
This is my query:
select tblSite.name,tblSite.site_length,tblSite.site_depth,tblSite.water_type,tblSite.country,
COUNT(distinct tblDivingClub.number) as number_of_clubs,
COUNT(distinct tblDiving.diving_number) as number_of_divings,
COUNT(distinct tblDiving.guide) as number_of_guids,
sum(tblDiving.number_of_divers) as number_of_divers,
(SELECT top 1 Z.dMonth,Z.dCount FROM (
SELECT COUNT(*) dCount, month(tblDiving.date_of_diving) as dMonth
FROM tblDiving
GROUP BY month(tblDiving.date_of_diving)
)Z
ORDER BY Z.dCount)
from tblCountry inner join
tblSite on tblCountry.name=tblSite.country
inner join tblDiving on tblSite.name = tblDiving.diving_site
inner join tblDivingClub on tblDiving.diving_club = tblDivingClub.number
where tblSite.name in(select tblSite.name from tblSite where tblSite.country = 'Greece' ) and
tblDiving.date_of_diving >= DATEADD(year,-1, GETDATE())
group by tblSite.name,tblSite.site_length,tblSite.site_depth,tblSite.water_type,tblSite.country
and now the error is: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS