請問一下 有表,根據它的id分組查詢 分組後的每個id 前4條資料,怎樣寫sql語句

時間 2021-10-14 22:24:01

1樓:擅長資料庫育兒

首先,按照id分組後的前4條資料是根據什麼字段排序取的前4條資料,假設情況如下。表有兩列,一列為id列,一列為排序列,假設為score。即表如下:

id score

1 99

1 98

1 97

1 96

1 95

2 100

2 87

2 96

……你所要的結果應該是這樣的?取按照課程號分組後,取每個課程號的前4個高分的情況。

select t.id,t.score from (select id,score,rank() over (partition by id order by score desc) ranks from 表) t where t.

ranks<=4;

以上語句在比如數分數為第三名的有兩個同學,那麼就會顯示兩條資料,而不是一條。如果沒有這樣的要求,可以使用rownum實現。

2樓:匿名使用者

select top(4) *from table group by id

sql查詢表中id最大的一條資料怎麼寫?

3樓:匿名使用者

你想搜尋欄位中最大的數?用這個select max(column_name) from table_name

4樓:陽光伴我們成長

應該加主鍵條件吧:select * from table a where id=(select max(id) from table where key =a.key ) and key ='n';

5樓:我親愛的的

select * from table where id=(select max(id) from table )

6樓:劉錢

select top 1 * from table order by id desc;

select * from table order by desc limit 1;

查詢乙個表中所有id欄位在另乙個表中對應值的sql語句怎麼寫?

7樓:匿名使用者

子查詢即可

select (select name from ta where id = tb.id1) as id1,

(select name from ta where id = tb.id2) as id2

from tb

8樓:匿名使用者

--建立bai

測試數du

據zhi

create table #a(id int,name varchar(10))

insert into #a values(1,'a')insert into #a values(2,'b')insert into #a values(3,'c')create table #b(id1 int,id2 int)insert into #b values(1,2)insert into #b values(2,1)insert into #b values(2,3)---------------實現dao

**部內

分容----------------

select

t1.name as name1

,t2.name as name2

from

#b tt

left join #a t1 on tt.id1 =t1.idleft join #a t2 on tt.id2 =t2.id

9樓:匿名使用者

select * from ta,tb where ta.id= tb.id1

10樓:匿名使用者

select c.name,d.name from (select a.

id1 id1,a.id2 id2,a.name name from ta a,tb b where a.

id1=b.id1) temp c

,ta d where c.id2=d.id2

11樓:匿名使用者

select t1.name as name1,t2.name as name2 from ta as t1,ta as t2,tb

where t1.id = tb.id1 and t2.id = tb.id2

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

12樓:大野瘦子

select count(*)

from (select 分組字段 from 表group by 分組字段

)別名或者 select count(*)

from (select distinct 分組字段 from 表)別名

13樓:突擊手覓患

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

select 分組字段 from 表

group by 分組字段

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

那就這樣

select count(*)

from (select 分組字段 from 表group by 分組字段

)別名或者

select count(*)

from (select distinct 分組字段 from 表)別名

14樓:匿名使用者

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

15樓:

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

select 分組字段 count(*)

from 表

group by 分組字段

16樓:順德迷途羔羊

select count(*) from 表

17樓:匿名使用者

假設表tablename欄位col1

select col1,count(col1)as [count] from tablename group by col1

18樓:談論使用者名稱

select count(*),列名 from 表名 group by 列名

19樓:佀重遙

select top 10 count(*) as num,id from table group by id order by num desc

查詢外加排序

幫我查一下有沒有這個註冊公司,幫我查一下有沒有這個註冊公司

通達建網路科技 深圳 的基本資訊 註冊號 440306503424263 企業中文名稱 通達建網路科技 深圳 住所 深圳市南山區公園南路蛇口聯合工業村g棟南山區電子商務創新服務基地a508 辦公場所 法定代表人 peng yuan 認繳註冊資本 港幣554.02萬元 認繳實收資本 港幣334.02萬...

幫我查一下有關諧音的笑話要笑話還有有

諧音笑話 一個鄉里看會,由於諧音。村長說 兔子們,蝦米們,不要漿瓜,鹹菜太貴了。同志們,鄉民們,不要講話,現在開會了 主持人說 鹹菜請香腸講話。現在請鄉長講話 鄉長說 兔子們,蝦米們,今天飯狗吃了,大家都是王八.同志們,鄉民們,今天的飯夠吃了,大家都用大碗吧 一個外國女孩嫁到中國來,在早飯時,對於不...

請問一下,有什麼優惠的流量套餐推薦嗎?

電信手機卡訊號穩定,網路速度快,而且 多,資費優惠,比如電信5g雲 步步高 等,不同地區具體的 有所不同,使用者可以到當地電信營業廳或者登入本省電信網上營業廳了解相關 詳情,也可以直接通過電信營業廳 網上營業廳渠道辦理 的,推薦使用者辦理電信5g雲 使用。中國電信的5g雲 總共分為7檔,129元 1...