用SQL语句查询最小值,最大值不能用min,max函数怎么查
展开全部
1.
--大于等于所有(最大值)
select * from Apo_city
where city_id >=all (select city_id from Apo_city)
--小于等于所有(最小值)
select * from Apo_city
where city_id <=all (select city_id from Apo_city)
--2.
--降序取第一个(最大值)
select * from Apo_city
where city_id = (select top 1 city_id from Apo_city order by city_id desc )
--升序取第一个(最小值)
select * from Apo_city
where city_id = (select top 1 city_id from Apo_city order by city_id Asc )
--3.
--最大值
select Top 1 city_id from Apo_city order by city_id desc
--最小值
select Top 1 city_id from Apo_city order by city_id Asc
--4.
--最大值
With T
As
(
select *,ROW_NUMBER() over(order by city_id Desc) as id from Apo_city
)
select * from T where id=1
--最小值
With T
As
(
select *,ROW_NUMBER() over(order by city_id Asc) as id from Apo_city
)
select * from T where id=1
5.
--不小于任何一个(最大值)
select * from Apo_city
where not city_id < any (select city_id from Apo_city )
--不大于任何一个(最小值)
select * from Apo_city
where not city_id > any (select city_id from Apo_city )
2015-12-07 · 知道合伙人软件行家
关注
展开全部
可通过对数据进行排序,再取第一条的方式获取最大值及最小值
示例如下:
--取最大值,对支付金额倒序排列,再取第一条
DECLARE @d DECIMAL
SELECT TOP 1 @d= payPrices FROM [dbo].[OrderBills] ORDER BY payPrices desc
SELECT @d --输出最大值
--取最小值,对支付金额顺序排列,再取第一条
DECLARE @d DECIMAL
SELECT TOP 1 @d= payPrices FROM [dbo].[OrderBills] ORDER BY payPrices
SELECT @d--输出最小值
示例如下:
--取最大值,对支付金额倒序排列,再取第一条
DECLARE @d DECIMAL
SELECT TOP 1 @d= payPrices FROM [dbo].[OrderBills] ORDER BY payPrices desc
SELECT @d --输出最大值
--取最小值,对支付金额顺序排列,再取第一条
DECLARE @d DECIMAL
SELECT TOP 1 @d= payPrices FROM [dbo].[OrderBills] ORDER BY payPrices
SELECT @d--输出最小值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询