select c1.departid 部門,
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供承德網(wǎng)站建設(shè)、承德做網(wǎng)站、承德網(wǎng)站設(shè)計、承德網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、承德企業(yè)網(wǎng)站模板建站服務,10年承德做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務。
max(case when
(select count(*) from company c2 where c1.departid=c2.departid and c1.payc2.pay)=0
then name
else '' end) 第一名,
max(case when
(select count(*) from company c2 where c1.departid=c2.departid and c1.payc2.pay)=1
then name
else '' end) 第二名,
max(case when
(select count(*) from company c2 where c1.departid=c2.departid and c1.payc2.pay)=2
then name
else '' end) 第三名
from company c1 group by departid order by departid
/
一樓邏輯有問題,這個SQL是先在表中取出前10行,在進行排序;
應該先對表排序,在取出前10行;
select * from a (select * from table order by xxx) a where rownum=10;
首先,來構(gòu)造一些數(shù)據(jù)
drop table test;
create table test
(
name varchar2(10),
account number(5)
);
insert into test values ('張三','5');
insert into test values ('王五','10');
insert into test values ('小二','10');
insert into test values ('李四','20');
insert into test values ('小三','40');
insert into test values ('小四','50');
insert into test values ('小五','90');
insert into test values ('小六','90');
insert into test values ('小七','90');
commit;
下面來看一下一些方式的排名結(jié)果:
1.? select??? ?t.*,dense_rank() over(order by t.account asc)?? 排名?? from test t;
2. select? ?t.*,dense_rank() over(order by t.account desc) 排名??? from??test t;
3. select t.*,rank() over(order by t.account asc) 排名 from test t;
4. select t.*,rank() over(order by t.account desc) 排名 from test t;
oracle查詢排名可以使用排名函數(shù)。
比如:RANK DENSE_RANK
參考語句:
--按照筆試成績進行排名,取出前兩名
select * from
(
SELECT p.writtenExam, i.stuName, i.stuClass,
RANK() OVER ( ORDER BY p.writtenExam desc ) 排名1
,DENSE_RANK() OVER ( ORDER BY p.writtenExam desc) 排名2
FROM stuInfo i
INNER JOIN stuMarks p
ON i.stuNo = p.stuNo
) f
where 排名1=2
oracle中前N條數(shù)據(jù)可用row_number來實現(xiàn)。
如表中數(shù)據(jù):
現(xiàn)在要求按照ID倒序,取出前十位:
oracle中執(zhí)行方法:
select?t.id,t.name?from
(select?test.*,row_number()?over?(order?by?id?desc)?rn?from?test)?t
where?rn=10;
結(jié)果:
select user_id_a, cnt
from (select user_id_a, cnt
from table
where user_id_a = user_id
order by user_id_a desc)
where rownum 4