服务器之家

服务器之家 > 正文

oracle排名函数的使用方法分享

时间:2019-12-10 15:03     来源/作者:mdxy-dxy

在oracle中,有rank,dense_rank,row_number,以及分组排名partition。

说明:

rank:排名会出现并列第n名,它之后的会跳过空出的名次,例如:1,2,2,4
dense_rank:排名会出现并列第n名,它之后的名次为n+1,例如:1,2,2,3
row_number:排名采用唯一序号连续值,例如1,2,3,4
partition:将排名限制到某一分组

格式:
 

row_number() over(partition by bb.channel_name order by sum(aa.dk_serv_num) desc nulls last) p1_rank1,
row_number() over(order by sum(aa.dk_serv_num) desc nulls last) rank1,
dense_rank() over(order by nvl(sum(aa.dk_serv_num), 0) desc) rank2,
rank() over(order by sum(aa.dk_serv_num) desc nulls last) rank3

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
procedure GetCompetitionRanking(p_UserId in integer, p_CompetitionId in integer, v_cursor out CompetitionCursor)
is
v_startDate date;
v_endDate date;
tmp_startDate varchar2(12);
tmp_endDate varchar2(12);
tmp_date date;
v_sql1 varchar2(2000);
v_sql2 varchar2(2000);
v_where varchar2(1000);
 
v_comTotal integer;
v_groupTotal integer;
v_comRanking integer;
v_groupRanking integer;
begin
select t.start_date, t.end_date into v_startDate, v_endDate from tbl_competition t where t.competition_id = p_CompetitionId;
 
tmp_date:= v_endDate+1;
tmp_startDate := to_char(v_startDate, 'yyyy-mm-dd');
tmp_endDate := to_char(tmp_date, 'yyyy-mm-dd');
 
--group personal total
select count(1) into v_groupTotal from tbl_com_group_user a
where a.com_group_id in
(
select b.com_group_id from tbl_com_group_user b where b.user_id = p_UserId
);
 
-- Competition personal total
select count(1) into v_comTotal from
(
select a.com_group_id from tbl_com_group a where a.competition_id = p_CompetitionId
) a inner join tbl_com_group_user b on a.com_group_id = b.com_group_id;
 
--user in competition ranking and group ranking
v_where := 't.DATA_TYPE_ID=1 AND t.STATUS=1 AND
t.DATA_DATE_1 >= TO_DATE('||chr(39)||tmp_startDate||chr(39)||','||chr(39)||'yyyy-mm-dd'||chr(39)||') AND
t.DATA_DATE_1 < TO_DATE('||chr(39)||tmp_endDate||chr(39)||','||chr(39)||'yyyy-mm-dd'||chr(39)||') ';
 
/*select no from
(
select a.USER_ID, dense_rank() over(order by sum(nvl(DATA_NUMBER_2, 0)) desc) no
from
(
select user_id from tbl_com_group a
left join tbl_com_group_user b on a.com_group_id = b.com_group_id
where a.competition_id = 1
) a left join VM_MASTER_DATA t on a.user_id = t.user_id and t.DATA_TYPE_ID=1 AND t.STATUS=1 AND
t.DATA_DATE_1 >= TO_DATE('2012-10-02','yyyy-mm-dd') AND
t.DATA_DATE_1 < TO_DATE('2012-12-01','yyyy-mm-dd')
group by a.user_id
order by no desc
) where user_id = 165*/
 
v_sql1 := 'select no from
(
select a.USER_ID, dense_rank() over(order by sum(nvl(DATA_NUMBER_2, 0)) desc) no
from
(
select user_id from tbl_com_group a
left join tbl_com_group_user b on a.com_group_id = b.com_group_id
where a.competition_id = '||p_CompetitionId||'
) a left join VM_MASTER_DATA t on a.user_id = t.user_id and '|| v_where||'
group by a.user_id
order by no desc
) where user_id = '||p_UserId;
 
dbms_output.put_line(v_sql1);
execute immediate v_sql1 into v_comRanking;
dbms_output.put_line('------------------------------');
--dbms_output.put_line(v_comRanking);
 
/*select no from
(
select a.USER_ID, dense_rank() over(order by sum(nvl(DATA_NUMBER_2, 0)) desc) no
from
(
select a.user_id from tbl_com_group_user a
where a.com_group_id in
(
select b.com_group_id from tbl_com_group_user b where b.user_id = 165
)
) a left join VM_MASTER_DATA t on a.user_id = t.user_id and t.DATA_TYPE_ID=1 AND t.STATUS=1 AND
t.DATA_DATE_1 >= TO_DATE('2012-10-02','yyyy-mm-dd') AND
t.DATA_DATE_1 < TO_DATE('2012-12-01','yyyy-mm-dd')
group by a.user_id
order by no desc
)
where user_id=165*/
 
v_sql2 := 'select no from
(
select a.USER_ID, dense_rank() over(order by sum(nvl(DATA_NUMBER_2, 0)) desc) no
from
(
select a.user_id from tbl_com_group_user a
where a.com_group_id in
(
select b.com_group_id from tbl_com_group_user b where b.user_id = '||p_UserId||'
)
) a left join VM_MASTER_DATA t on a.user_id = t.user_id and '|| v_where||'
group by a.user_id
order by no desc
) where user_id = '||p_UserId;
 
dbms_output.put_line(v_sql2);
execute immediate v_sql2 into v_groupRanking;
--dbms_output.put_line('------------------------------');
--dbms_output.put_line(v_groupRanking);
 
if v_comRanking is null then
v_comRanking := v_comTotal;
end if;
 
if v_groupRanking is null then
v_groupRanking := v_groupTotal;
end if;
 
open v_cursor for
select v_comTotal CompetitionPersonalTotal, v_groupTotal UserInGroupPersonTotal, v_comRanking UserInCompRanking, v_groupRanking UserInGroupRanking from dual;
 
exception
when others then
null;
end;
标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部