sql 分組查詢前5的記錄

時間 2025-04-08 19:50:17

1樓:匿名使用者

用group by查詢。select top 5 班級,學號,分數 from 學生表group by 班級,學號,分數。

_top 5表示頭5行資料。也可以用百分百表示 如果你的表中有100行資料,要查5行,則用select top 5percent 班級,學號,分數。

from 學生表group by 班級,學號,分數。

2樓:匿名使用者

有相同分數的情況:select a.班級,a.

學號,a.分數 from 學生表 a, (select top 5 分數,班級 from 學生表 group by 分數,班級 order by 分數 desc) t where a.班級=t.

班級 and a.分數》=(select min(分數) from t)

3樓:匿名使用者

樓上2位有測試過嗎?嘛,一樓肯定不行,因為你只返回了5條資料,人家是每個班級5條。所以你已經錯了。2樓也一樣,只會返回5條資料,還有更好的答案嗎。路過學習中。。

sql分組統計後分別顯示前幾條記錄

4樓:

首先,該問題對應的sql如下。

select 採購類別,客戶,訂貨總額。

from (select 採購類別,客戶,訂貨總額,row_number() over(partition by 採購類別 order by 訂貨總額 desc) rn

from table_name) a

where rn<=2

其次,常用資料庫比如oracle和sqlserver都有特定函式完成分組排序的功能,如果需要顯示並列的情況可以用下面另外的2個。

分別有3個類似函式:

row_number() over

這個函式不需要考慮是否並列,哪怕根據條件查詢出來的數值相同也會進行連續排名。也是最常用的函式,排序結果類似於1,2,3,4,5

rank() over

查出指定條件後進行乙個排名,但是有乙個特點。假如是對學生排名,那麼實用這個函式,成績相同的兩名是並列。排序結果類似於1,2,2,4,5

dense_rank() over

比較特殊,排序結果類似於1,2,2,3,4

5樓:突擊手覓患

先預設你的資料已經分組合並過了。

這裡你說的前2條也沒說明是按什麼排名的,姑且當你按訂貨總額由高到低吧。

select 採購類別,客戶,訂貨總額。

fromselect row_number() over(partition by 採購類別 order by 訂貨總額 desc) rn,採購類別,客戶,訂貨總額 from 表或查詢) a

where rn<3

sql語句 如何分組後得到記錄總數

6樓:大野瘦子

select count(*)

from (select 分組欄位 from 表group by 分組欄位。

別名或者 select count(*)

from (select distinct 分組欄位 from 表)別名。

7樓:突擊手覓患

要得到每組的合計可以用樓,要總計用1樓,想一次性得到分組合計以及總計,sql2005可以這麼寫:

select 分組欄位 from 表。

group by 分組欄位。

compute sum(count(*)*****

那就這樣。select count(*)

from (select 分組欄位 from 表group by 分組欄位。

別名或者。select count(*)

from (select distinct 分組欄位 from 表)別名。

8樓:網友

統計記錄條數就用count()函式啊,如果想統計分組以後每個組的記錄數,就分別統計或者在where條件中加上分組條件。

9樓:

使用函式count(*)就可以啊。要是統計總和就用sum。

select 分組欄位 count(*)

from 表。

group by 分組欄位。

10樓:網友

dense_rank()有bug

譬如分數100, 100, 99, 88, 77, 66如果用dense的話,會返回以上6條記錄,而樓主的意思是要返回100, 100, 99, 88, 77.

select *

from (select rank() over(partition by 班級 order by 分數 desc) as denserank,* from table1 ) as a

where <=5

11樓:

終於找到踩專家的機會了。

select *

from 表 a

where exists(

select 1 from

select top 5 percent 學號,班級 from 表 b

where a.班級=b.班級 order by 分數 desc)cwhere c.學號=a.學號 and c.班級=a.班級)

12樓:網友

微軟專家吳熹分數排序錯誤,學習了。我做的複雜多了select *

from (select dense_rank()over(partition by 班級 order by 分數 desc) as denserank,* from table1 ) as a

where denserank<=5

--select e.班級, as 名次, d.學號 as 學號, d.分數 as 分數。

fromselect a.班級,a.學號,count(distinct b.

分數)+1 as place from @tb a left join @tb b on a.分數 < b.分數 and a.

班級 = b.班級 group by a.班級,a.

學號) as e inner join @tb as d on e.學號 = d.學號 and e.

班級 = d.班級。

where <=5

sql 分組查詢 分組查詢查詢每組的前幾條資料

13樓:網友

sqlserver:

select * from (

select *,row_number() over(partition by 分組。

抄列襲 order by 組內排序列) as rn from table

as a where rn<=2

sql排序分組取數的問題,乙個sql排序分組取數的問題

首先,因為不知道您用的是什麼資料庫。如下,僅以oracle為例,希望可以觸動您的靈感,sql如下 select t2.from select t1.rank over partition by t1.編號 order by t1.日期 desc as rk from contract t1 t2 w...

sql 語句 從所查詢得到的表中 提取前幾條 怎麼寫

看你用的什麼資料庫了,一般sql server可以用。select top from select substring newstitle,, from newstablelist where newsclassid and channelid order by substring newstitl...

sql查詢如何去除重複的記錄,SQL查詢,如何去除重複的記錄?

上面的回答可以,不過我覺得還有更簡單的方法 select aid,count distinct uid from 表名 group by aid 這是sqlserver 的寫法。 樂園小 select distinct 文章aid,使用者uid from table名 select from tab...