1樓:郭某人來此
sql取分組中的前幾名
[sql] www.2cto.com
create table #aa(class varchar(10),name varchar(10),lang int,math int)
goinsert into #aa
select '1','a1',60,70union all
select '1','a2',80,70union all
select '1','a3',90,70union all
select '1','a4',50,70go
insert into #aa
select '2','b1',60,70union all
select '2','b2',90,90union all
select '2','b3',90,70union all
select '2','b4',50,70go
select * from #aa
--取每年級前一名的成級
select * from
(select row_number() over(partition by class order by lang+math desc) rr, * from #aa ) a
where rr<2
--取每年級前二名的成級
select * from
(select row_number() over(partition by class order by lang+math desc) rr, * from #aa ) a
where rr<3
2樓:
找了兩個方法,參考一下
--方法二:
select * from @源 a where not exists (select 1 from @源 where 組=a.組 and id 1)
--方法三:
select * from @源 a where (select count(*) from @源 where 組=a.組 and id
sql資料庫怎麼實現分組並取每組的前1條語句,按日期排序?
3樓:果樹上的小黑貓
select * from
(select row_number() over(partition by '分組' order by '日期') as rownum -- 排序並分組
, * -- 所需顯抄示的字段from 表
) as t
where t.rownum = 1
對每組的資料按日期排序並加上行號
取出時只取行號為1,也就是第一條資料。
4樓:匿名使用者
select top 1 * from 表 group by 分組條件 order by 日期
sql怎麼取出每個科目前三名並按科目和分組排列
5樓:芯勢慮輪壞伊
select b1.姓名,b1.科目,b1.
分數 from b b1 where(select count(1) from b where 科目=b1.科目 and 分數〉=b1.分數)〈=3 order by b1.
科目,b1.分數;
6樓:匿名使用者
select rank() over(patrition by 科目 order by 分數) 排名,姓名,科目,分數
from b;
沒做測試資料,不好使再問我吧。
資料庫用的oracle 11g
7樓:匿名使用者
select bb.科目,bb.rk,bb.姓名,bb.分數from (select b.*,
rank() over(partition by b.科目 order by b.分數 desc) rk
from b )bb
where bb.rk < 4;
oracle 10g 已經測試。
8樓:三太狼童裝
select * from b order by 科目 desc, 分數 desc
sql先分組後排序的問題
9樓:匿名使用者
select regtime,ccount,row_number()over(partition by case when ccount=0 then 0 else 1 end order by regtime) from 表
sql分組排序
10樓:非典型天秤座
group by語句必須和聚合函式一起使用.
select c,max(d) from a group by c order by max(d) desc
這樣子可以.
因為一條select語句只可以返回乙個結果集...
此句返回按c分組後並按每組中最大的d值進行排序.
11樓:遲賜阿軼麗
華子,我來看你.....
12樓:匿名使用者
不用group by吧樓上的..直接orderby c,d.就好了吧..
可以直接試試
select * from a order by c,d;
sql分組統計後分別顯示前幾條記錄
13樓:
首先,該問題對應的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
14樓:突擊手覓患
先預設你的資料已經分組合併過了。
這裡你說的前2條也沒說明是按什麼排名的,姑且當你按訂貨總額由高到低吧。
select 採購類別,客戶,訂貨總額
from
(select row_number() over(partition by 採購類別 order by 訂貨總額 desc) rn,採購類別,客戶,訂貨總額 from 表或查詢) a
where rn<3
SQL如何根據表的內容對另表進行排序
select a.from product a,facture b where a.factureid b.factureid order by b.facturename select product.from product a,facture bwhere a.factureid b.fact...
如何面對分手後的難過,分手後怎麼可以不難過?
斷聯。斷聯可以避免你在走出失戀的過程中陷入惡性迴圈往復當中。不要開始新戀情。開始新戀情只會讓你感到更加空虛。想哭就哭。可以哭,無論在什麼地方,戴個口罩就可以哭出來,不要不好意思。可以借酒消愁。前期適當的可以喝酒,但要注意身體。可以去挽回。如果你不甘心,嘗試著去挽回,但不要頻繁,想好挽回技巧 如果兩次...
請教甜瓜嫁接後管理,如何進行甜瓜嫁接後的管理?
專家解答 自嫁接之日起,靠接苗經8 10天,插接苗經10 12天,即可斷定成活與否。在此期間加強對嫁接苗的溫度 濕度和光照等管理,對提高嫁接成活率具有決定性的作用。1 溫度管理嫁接苗癒合的最適宜苗床溫度為白天25 左右,夜間15 以上,地溫20 左右。應採用雙層薄膜小拱棚覆蓋,或採用電熱溫床,以便控...