计算差异表中的所有最大值 sql
Count all max number value in difference tables sql
我在尝试解决这个问题时遇到了错误。首先,我需要计算 2 tables 的所有值,然后我需要在 where 条件下获取所有最大值。
我的代码:
Select *
FROM (
select Operator.OperatoriausPavadinimas,
(
select count(*)
from Plan
where Plan.operatoriausID= Operator.operatoriausID
) as NumberOFPlans
from Operator
)a
where a.NumberOFPlans= Max(a.NumberOFPlans)
我收到这个错误
Msg 147, Level 15, State 1, Line 19
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
我不知道怎么解决。
我需要得到这个http://prntscr.com/p700w9
更新 1
计划 table 包含 http://prntscr.com/p7055l 个值和
运算符 table 包含 http://prntscr.com/p705k0 个值。
您是否正在寻找...连接两个表和 returns 具有最大计数的记录的聚合查询?
我怀疑这句话可能如下:
SELECT TOP(1) o.OperatoriausPavadinimas, COUNT(*)
FROM Operatorius o
INNER JOIN Planas p ON p.operatoriausID = o.operatoriausID
GROUP BY o.OperatoriausPavadinimas
ORDER BY COUNT(*) DESC
如果你想允许平局,你可以使用TOP(1) WITH TIES
。
您可以使用 top with ties
。您的查询有点难以理解,但我想您想要:
select top (1) with ties o.OperatoriausPavadinimas, count(*)
from plan p join
operator o
on p.operatoriausID = o.operatoriausID
group by o.OperatoriausPavadinimas
order by count(*) desc;
我在尝试解决这个问题时遇到了错误。首先,我需要计算 2 tables 的所有值,然后我需要在 where 条件下获取所有最大值。
我的代码:
Select *
FROM (
select Operator.OperatoriausPavadinimas,
(
select count(*)
from Plan
where Plan.operatoriausID= Operator.operatoriausID
) as NumberOFPlans
from Operator
)a
where a.NumberOFPlans= Max(a.NumberOFPlans)
我收到这个错误
Msg 147, Level 15, State 1, Line 19 An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
我不知道怎么解决。
我需要得到这个http://prntscr.com/p700w9
更新 1
计划 table 包含 http://prntscr.com/p7055l 个值和 运算符 table 包含 http://prntscr.com/p705k0 个值。
您是否正在寻找...连接两个表和 returns 具有最大计数的记录的聚合查询?
我怀疑这句话可能如下:
SELECT TOP(1) o.OperatoriausPavadinimas, COUNT(*)
FROM Operatorius o
INNER JOIN Planas p ON p.operatoriausID = o.operatoriausID
GROUP BY o.OperatoriausPavadinimas
ORDER BY COUNT(*) DESC
如果你想允许平局,你可以使用TOP(1) WITH TIES
。
您可以使用 top with ties
。您的查询有点难以理解,但我想您想要:
select top (1) with ties o.OperatoriausPavadinimas, count(*)
from plan p join
operator o
on p.operatoriausID = o.operatoriausID
group by o.OperatoriausPavadinimas
order by count(*) desc;