用SQL语句查询最小值,最大值不能用min,max函数怎么查
答案:2 悬赏:30 手机版
解决时间 2021-04-05 23:11
- 提问者网友:听门外雪花风
- 2021-04-05 08:22
用SQL语句查询最小值,最大值不能用min,max函数怎么查
最佳答案
- 五星知识达人网友:轻熟杀无赦
- 2021-04-05 09:48
可通过对数据进行排序,再取第一条的方式获取最大值及最小值
示例如下:
--取最大值,对支付金额倒序排列,再取第一条
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--输出最小值
全部回答
- 1楼网友:孤独的牧羊人
- 2021-04-05 10:57
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 )
--大于等于所有(最大值)
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 )
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯