Sql 我需要从表中获取max row

Sql 我需要从表中获取max row,sql,Sql,我有这样的桌子: id | idx | cost 1 1 500 2 1 1000 3 2 500 4 2 9000 我需要获取id=@id的最大成本 所以我写了这个查询: SELECT Id FROM table WHERE (cost= (SELECT MAX(cost) AS Expr1 FROM table AS table_1)) AND (idx= @idx) 但这只有在成本最高的情况下才有效 我需要获取max

我有这样的桌子:

id | idx | cost
1    1     500
2    1     1000
3    2     500
4    2     9000
我需要获取id=@id的最大成本

所以我写了这个查询:

SELECT   Id
FROM    table
WHERE (cost= (SELECT MAX(cost) AS Expr1 FROM  table AS table_1)) AND (idx= @idx)
但这只有在成本最高的情况下才有效

我需要获取max cost的id,其中idx=@idx

只需使用order by和一些东西就可以将其保存到一行:

select t.*
from t
where id = @id
order by cost desc
fetch first 1 row only;
只需使用order by和某物将其固定到一行:

select t.*
from t
where id = @id
order by cost desc
fetch first 1 row only;
使用top 1子句:

使用top 1子句:

使用以下命令:

从idx=@idx和cost=的表中选择*从idx=@idx的表中选择MAXcost

使用以下命令:


从idx=@idx和cost=的表中选择*从idx=@idx的表中选择MAXcost

这真的和C有关吗,或者这个标签是错误的吗?这真的和C有关吗,或者这个标签是错误的吗?很多,我的问题解决了很多,我的问题解决了很多,我的问题解决了很多,我的问题解决了很多,我的问题解决了很多