當前位置:首頁 » 編程語言 » sql語句查詢最小值
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql語句查詢最小值

發布時間: 2022-06-26 17:25:11

㈠ 求sql語句 多列取最小值

請查閱這里:求最小值的方法

裡面舉三個例子:

1 使用values子句生成臨時表

2使用行列轉換

3使用union all拼接臨時表

createtabletest
(namevarchar(10),time1int,time2int,time3int)
insertintotest(name,time1,time2,time3)
values
('a',1,2,3), ('b',8,9,6), ('c',11,22,8), ('d',101,201,38),
('e',6,7,9), ('f',8,8,13), ('g',2,2,30), ('h',82,56,53)
go

---方法1:使用values子句構建臨時表

selectname,(selectmin(timeMin)from(values(time1),(time2),(time3))as#temp(timeMin))astimeMinfromtest

---方法2行轉列

selectname,min(timeMin)as[最小數]fromtestunpivot(timeMinfortimeMintin(time1,time2,time3))asugroupbyname

--方法3:使用unionall組合新表
selectname,(selectmin(timeMin)
as[最小數]from(
selecttest.time1astimeMin
unionall
selecttest.time2
unionall
selecttest.time3)ud)
MaxDatefromtest

go

droptabletest

如有疑問,及時溝通!

㈡ sql求某一欄位中最大值和最小值的問題,高手請進!

sql查詢欄位的最大值使用max()函數。

例:select

max(a)

from

table

語句大意:檢索表table中a欄位中的最大值。

(2)sql語句查詢最小值擴展閱讀:

1、SQL數據定義功能:能夠定義資料庫的三級模式結構,即外模式、全局模式和內模式結構。在SQL中,外模式又叫做視圖(View),全局模式簡稱模式( Schema),內模式由系統根據資料庫模式自動實現,一般無需用戶過問。

2、SQL數據操縱功能:包括對基本表和視圖的數據插入、刪除和修改,特別是具有很強的數據查詢功能。

3、SQL的數據控制功能:主要是對用戶的訪問許可權加以控制,以保證系統的安全性。

㈢ sql查詢時間最小值的列

可以參考下面的方法:

1、將查詢的結果按照時間列從小到大排序,也就是正序排序,只取第一條就行

SELECT TOP 1 * FROM tb ORDER BY 時間列 ;

2、另外可以使用子查詢

SELECT * FROM tb WHERE 時間列=(SELECT MIN(時間列) FROM tb);

(3)sql語句查詢最小值擴展閱讀:

SQL參考語句

AVG(欄位名) 得出一個表格欄平均值

COUNT(*;欄位名) 對數據行數的統計或對某一欄有值的數據行數統計

MAX(欄位名) 取得一個表格欄最大的值

MIN(欄位名) 取得一個表格欄最小的值

Alter table tabname add primary key(col)添加主鍵

Alter table tabname drop primary key(col)刪除主鍵

㈣ 用SQL語句查詢最小值,最大值不能用min,max函數怎麼查

1.
--大於等於所有(最大值)
select*fromApo_city
wherecity_id>=all(selectcity_idfromApo_city)
--小於等於所有(最小值)
select*fromApo_city
wherecity_id<=all(selectcity_idfromApo_city)

--2.
--降序取第一個(最大值)
select*fromApo_city
wherecity_id=(selecttop1city_idfromApo_cityorderbycity_iddesc)
--升序取第一個(最小值)
select*fromApo_city
wherecity_id=(selecttop1city_idfromApo_cityorderbycity_idAsc)

--3.
--最大值
selectTop1city_idfromApo_cityorderbycity_iddesc
--最小值
selectTop1city_idfromApo_cityorderbycity_idAsc

--4.
--最大值
WithT
As
(
select*,ROW_NUMBER()over(orderbycity_idDesc)asidfromApo_city
)
select*fromTwhereid=1
--最小值
WithT
As
(
select*,ROW_NUMBER()over(orderbycity_idAsc)asidfromApo_city
)
select*fromTwhereid=1

5.
--不小於任何一個(最大值)
select*fromApo_city
wherenotcity_id<any(selectcity_idfromApo_city)

--不大於任何一個(最小值)
select*fromApo_city
wherenotcity_id>any(selectcity_idfromApo_city)

㈤ SQL分組查詢最大值最小值

查詢語句參考如下:
select
UserName,--
Convert(char(8),dealTm,112) as date,--日期
min(dealTm) as earliestTime ,--最大
max(dealTm) as latestTime--最小
from t2
group by Convert(char(8),dealTm,112) ,UserName

㈥ 求:從查詢結果里,再查詢最小值的SQL語句

select year month from 表格名
where month=(select Min(month) from 表格名 where year=(select Min(year) from 表格名))

㈦ 如何用sql語句查出最大值,最小值等

oracle SQL語法
select max(t.num),min(t.num) from tableName t

㈧ sql查詢最小值,sql小白求助。

select 序號,min(值1)as 值1,min(值2) as 值2
from tb
groupby 序號!

㈨ SQL從查詢結果中查最小值

SELECT C.CategoryID, C.CategoryName, SUM(F.CommentNO) AS SumComment
FROM Category AS C, Feedback AS F, Article AS A
WHERE C.CategoryID=A.CategoryID AND A.ArticleID=F.ArticleID
GROUP BY C.CategoryID, C.CategoryName having SUM(F.CommentNO)=
(select min(t1.SumComment1) from
(SELECT SUM(F.CommentNO) AS SumComment1
FROM Category AS C, Feedback AS F, Article AS A
WHERE C.CategoryID=A.CategoryID AND A.ArticleID=F.ArticleID
GROUP BY C.CategoryID, C.CategoryName) as t1)
這樣試試

㈩ 如何用sql語句查出最大值、最小值等

select max(t) as 溫度最大值,min(t) as 溫度最小值,max(p) as 濕度最大值,min(p) as 濕度最小值,max(datetime) as 最後出現時間,min(datetime) as 最早出現時間 from yourtable_name