Ⅰ 用sql語句查詢表中第N(1,2,3等)大的數
--我這里一共有13個數,1到13
--這句查出來就是13
select max(id) from cardInfo
--這句查出來就是1
select min(id) from cardInfo
--這句查出來的是比13小的數
select id from cardInfo where id < (select max(id) from cardInfo)
--比如現在就要取第二大,也就是12
select top 1 id from cardInfo where id < (select max(id) from cardInfo) order by id desc
--現在取第三大的數,11
select top 1 id from cardInfo where id < (select max(id) from cardInfo where id < (select max(id) from cardInfo)) order by id desc
--同理現在可以取第四大了,10
select top 1 id from cardInfo where id < (select max(id) from cardInfo where id < (select max(id) from cardInfo where id < (select max(id) from cardInfo))) order by id desc
把他復制到查詢分析器裡面能看得更清楚.
Ⅱ 求某列第二最大值所在行的SQL
先從大到小排名,然後取第二名,那樣就是第二大的值了
例:
SELECT
*
FROM
(
select *,
rank() over ( ORDER BY id asc) rank1
from people
) a
where rank1=2
Ⅲ 取每組的第二最大值,如何使用SQL實現
分別取每組的第二最大值,如何使用SQL實現:
select * from
(
select row_number() over(partition by '分組' order by '日期') as rownum --
排序並分組
, * -- 所需顯示的欄位
from 表
) as T
where T.rownum = 1
對每組的數據按日期排序並加上行號
取出時只取行號為1,也就是第一條數據。
Ⅳ 有一列數 從中選出第二大的數 sql
select max(A),max(B) from table_name; 這樣就可以了
Ⅳ 如何一條sql語句查找表中第二大值
select max(value) from customer 返回的是包括最大值的表 ,是不能與一個值比較的,應該用 in 或 not in操作符,即:
select max(value) from customer where value not in (select max(value) from customer)
在查詢中,in 或 not in操作符是索引失效,速度變慢,可以用以下表連接的方法,
select max(value) from (select value from customer) as a left join (select max(value) as c from customer) as b on b.c=a.value where b.c is null
一般來說,以上兩種方法的執行速度 表連接的方法好些,但也應該進行測試,我認為,採用兩次查詢的方法比較合適,select max(value) from customer 得到最大值,
select max(value) from customer where value <6003
得到次大值。
Ⅵ SQL查詢第二大值
可以利用排序取,用row_number()函數,將個數重大到小排名,然後嵌套一層子查詢,where 條件中取 rank=2的就是第二大
Ⅶ Oracle資料庫怎麼列出某一列第二大的值
下面的SQL語句去掉重復值按照col列降序排序,第二行就是第二大的值
selectdistinctcolfromtable_nameorderbycoldesc
Ⅷ SQl里查詢不同數組的最大值(或排名前2)
/*查詢銷售業績最高的人*/
SELECT * FROM table
WHERE money IN (SELECT MAX(money) FROM table)
/*查詢銷售業績(總和)前二的公司*/
SELECT TOP 2 SUM(money),company
FROM table
GROUP BY company
其中table對應該表名,money對應銷售業績列名,company對應分公司列名。
Ⅸ SQL第2高的薪水
你可以展開來看,這樣容易理解,我將你子查詢等價展開
其實那個子查詢查出來的結果就是這樣,因為你條件是E2表大於E1表的工資,所以只看右邊那兩條數據。然後數字1就代表條數,意思就是在這個查詢中找一條記錄符合的。那就是200了,因為100 是有2條記錄。所以這樣就找到第二大的。同理你可以把1換成2,那結果就是100。再換成0,那結果就是300,因為300不在這里