【发布时间】:2014-08-09 18:19:04
【问题描述】:
我收到此错误:
ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效,除非还指定了 TOP 或 FOR XML
从这个查询:
(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)
我知道我需要在此查询中的某处添加“Top”,但在何处? 最后,这个查询需要返回Maximum dCount和这个dCount的月份。
已编辑:
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
现在的错误是:当没有用EXISTS引入子查询时,选择列表中只能指定一个表达式
【问题讨论】:
-
(SELECT TOP 1 Z.dMonth...
标签: sql-server