首页 > 解决方案 > 如何在没有最大值的情况下获得 SQL Server 上字段的最大值?

问题描述

我需要获取表字段的最大值,但我不能使用 max 或任何其他聚合函数,也不能使用游标。例如,我需要获取表 Sales 上字段金额的最大值。

标签: sqlsql-server

解决方案


有几种方法:
1.对列进行降序排序并获得第一行:

select top 1 amount from sales order by amount DESC

2.NOT EXISTS

select distinct s.amount 
from sales s 
where not exists (
  select 1 from sales
  where amount > s.amount
)

推荐阅读