四大排序函式

2021-09-08 04:22:09 字數 2831 閱讀 3149

row_number() over(partition by clause order by filed asc|desc )----連續的從1開始到n的排序號

[sql] view plain copy

rank() over ( partition by clause order by filed asc|desc)---不連續的可以出現重複排序號

[sql] view plain copy

dense_rank() over (partition by clause order by filed asc|desc)--連續的可以出現重複排序號

[sql] view plain copy

ntile (integer_expression) over (partition by clause order by filed asc|desc )--連續的可以分組排序並且排序號是連續的

[sql] view plain copy

----案例

create table tb (

[year] [int] not null,

[week] [int] not null,

[base_goods_id] [int] not null,

[uid] [bigint] not null,

[amount] [money] not null,

[count] [int] not null,

[bean] [money] not null

) insert tb

select 2013,2,6577,20087,32640.00,1088,26112.00

union all

select 2013,2,6577,20211,39420.00,1314,31536.00

union all

select 2013,2,6577,20220,60.00,2,111.00

union all

select 2013,2,6577,20457,60.00,2,48.00

union all

select 2013,2,6577,20458,60.00,2,48.00

go select * from tb

/* 2013 2 6577 20087 32640.00 1088 26112.00

2013 2 6577 20211 39420.00 1314 31536.00

2013 2 6577 20220 60.00 2 111.00

2013 2 6577 20457 60.00 2 48.00

2013 2 6577 20458 60.00 2 48.00

*/ select row_number() over(partition by base_goods_id order by count)rn,* from tb

/* rn year week base_goods_id uid amount count bean

1 2013 2 6577 20220 60.00 2 111.00

2 2013 2 6577 20457 60.00 2 48.00

3 2013 2 6577 20458 60.00 2 48.00

4 2013 2 6577 20087 32640.00 1088 26112.00

5 2013 2 6577 20211 39420.00 1314 31536.00

*/ select dense_rank()over(partition by base_goods_id order by count )rn,* from tb

/* rn year week base_goods_id uid amount count bean

1 2013 2 6577 20220 60.00 2 111.00

1 2013 2 6577 20457 60.00 2 48.00

1 2013 2 6577 20458 60.00 2 48.00

2 2013 2 6577 20087 32640.00 1088 26112.00

3 2013 2 6577 20211 39420.00 1314 31536.00

*/ select rank()over(partition by base_goods_id order by count )rn,* from tb

/* 1 2013 2 6577 20220 60.00 2 111.00

1 2013 2 6577 20457 60.00 2 48.00

1 2013 2 6577 20458 60.00 2 48.00

4 2013 2 6577 20087 32640.00 1088 26112.00

5 2013 2 6577 20211 39420.00 1314 31536.00

*/ select ntile(2)over(partition by base_goods_id order by count )rn,* from tb

/* rn year week base_goods_id uid amount count bean

1 2013 2 6577 20220 60.00 2 111.00

1 2013 2 6577 20457 60.00 2 48.00

1 2013 2 6577 20458 60.00 2 48.00

2 2013 2 6577 20087 32640.00 1088 26112.00

2 2013 2 6577 20211 39420.00 1314 31536.00

*/

SQL四大排序函式

sql四大排序函式 1 row number 函式作用就是將select查詢到的資料進行排序,每一條資料加乙個序號 select row number over order by score desc as rank from scores select from select row number...

javascript常見四大排序

氣泡排序 var arr 23,34,11,22,19,18 氣泡排序的思路分析 第1輪 第1步 23,34,11,22,19,18 第1輪 第2步 23,11,34,22,19,18 第1輪 第3步 23,11,22,34,19,18 第1輪 第4步 23,11,22,19,34,18 第1輪 第...

php四大排序之氣泡排序

一 氣泡排序 原理 對一組資料,比較相鄰資料的大小,將值小資料在前面,值大的資料放在後面。以下都是公升序排列,即從小到大排列 舉例說明 arr array 6,3,8,2,9,1 arr 有6個資料,按照兩兩比較大小如下,注意 比較輪數 和 每輪比較次數 第一輪排序 第一次比較 6和3比較 結果 3...