? 宝贝腿开大点我添添公视频免_postgreSQL中的row_number() 與distinct用法說(shuō)明-天津九安特機電工程有限公司

一本色道久久综合狠狠躁篇|亚洲av无码一区二区乱子伦as|亚洲国产成AV人天堂无码|亚洲狠狠婷婷综合久久蜜芽|狠狠做五月深爱婷婷|人妻夜夜爽天天爽三区

?

postgreSQL中的row_number() 與distinct用法說(shuō)明

這篇文章主要介紹了post(′?`)greSQL中的法說(shuō)row_n??umber() 與distinct用法說(shuō)明,具有很好的法說(shuō)參考??價(jià)值,希望對大家有所幫助。法說(shuō)一起跟隨小編過(guò)來(lái)看看吧。法說(shuō)

我就廢話(huà)不多說(shuō)了,法說(shuō)大家還是法說(shuō)直接看代碼吧~

select count(s.*)
from (
select *, row_number() over (partition by feヽ(′▽?zhuān)?/e_date order by fee_date) as gr
f( ?° ?? ?°)rom new_order where news_id='novel' and order_status='2'
) s
where s.gr = 1
SELECT count(DISTINCT fee_date) as dis from new_order wher??e news_id='novel' and order_status='2'

這兩個(gè)SQL執行所得到的數據是一樣的!

工具:postgreSQL

1.我們要清楚,法說(shuō)sql的法說(shuō)執行順序:

from語(yǔ)句->where語(yǔ)句->group by語(yǔ)句->having語(yǔ)句->order by( ???)語(yǔ)句->select 語(yǔ)句

2.row_number()分析函??數

說(shuō)明:返回結果集分區內行的序列號,每個(gè)分區的法說(shuō)第一行從?? 1 開(kāi)始。

語(yǔ)法:ROW_NUMBER () OVER ([ <partition_by_clause>]ヾ(′▽?zhuān)??<order_by_clause> )

備注:ORDER??BY 子句可確定在特定分區中為行分配唯一 ROW_NUMBER 的法說(shuō)順序。

參數:<partition_by_clause> :將FROM 子句生成的法說(shuō)結果集劃入應用了 ROW_NUMBER 函數??的分區。

<( ???);order_by_clause>:確定將 ROW_NUMBER 值分配給分區中行( ???)的法??說(shuō)順序。

返回類(lèi)型:bigint 。法說(shuō)

r(′?ω?`)ow_number()從1開(kāi)始,法說(shuō)為每一條分組記錄返回一個(gè)數字

1select *,法說(shuō) row_number()(′?`*) over (order by fee_date) from new_order

先把 fee_date 升序排列,再為升序以后的每條記錄返回一個(gè)序號

1select *, row_number() over (partition by fee(°ロ°) !_date order by fee_??date) as gr from new_or(╯°□°)╯︵ ┻━┻der

表示根據fee_date分組,在分組內部根據 fee_date排序,而此函數計算的值就表示每組內部排序后的順序編號(組內連續的唯一的)

2.distinct

語(yǔ)法ヽ(′ー`)ノ:

1SELECT DISTINCT 列名稱(chēng) FROM 表名稱(chēng)

distinct這個(gè)關(guān)鍵字用來(lái)過(guò)濾掉多余的重復記錄只保留一條

1selec??t DISTINCT fee_date from new_order

1select DISTINCT fee_date,order_status from ne(╬?益?)w_order

從結果可以看出,是根據“fee_(′▽?zhuān)?)date+order_status”來(lái)去重復數據的,distinct同時(shí)作用在了fee_date和order_status上

1SELECT count(D(′ω`*)ISTINCT fee_date) as dis from new_order where news_id='novel(′▽?zhuān)?' and order_status='2'

1select id,distinct fee_date from?? new_order ; –會(huì )提示錯誤,因為distinct必須放在開(kāi)頭

distヾ(′?`)?inct語(yǔ)句中(zhong)select顯示的字段只能是distinct指定的字段,其他字段是不可能出現的

補充:PostgreSQL RO???W_NUMBER()ヾ(′▽?zhuān)?? OVER()

我就廢話(huà)不多ヽ(′▽?zhuān)?ノ說(shuō)了,大家還是直接看代碼吧~

SELECT
*
FROM
(
SELE??CT
tt.s_ci s_ci,
sm.ci,
-- getdistance (
-- tt.longitude,
-- tt.latitude,
-- sm??.longitude,
-- sm.latitude
-- ) dista??nce,
ROW_N(′▽?zhuān)?UMBER () OVER (
PARTITION BY tt.s_ci
ORDER BY
getdistance (
tt.longitude,
tt.latitude,
sm.longitude,
sm.latitude
)
) rn
FROM
sm_cl_locヽ(′▽?zhuān)?ノation sm
INNER JOIN (
S??ELECT
s_ci,
longitude,
latitude,
n3_pci,
n3_earfcn
FROM
plan_ott_data
WHERE
1 = 1
AND (
s_ci = '460-00-1012286??-(╯°□°)╯2'
OR s_ci = '460-00-25514-130'
)
AND rpt_time BETWEEN '2018-04-30'
AND '2018-05-29'
) tt ON sm.pci = tt.n3_pci
AND sm.han??nel_number = tt.n3_earfcn
) T
WHERE
T .rn BETWEEN 1 and 3

語(yǔ)法:

1ROW_NUMBER() OVER( [ PRITI??TION BY col1] ORDER BY col2[ DESC ] )

解釋?zhuān)?/p>

ROW_NU??MBER()為返回的記錄定義個(gè)行編號, PARTIT??I??ON BY col1 是根據col1分組,ORDER BY col2[ DESC ]是根據col2進(jìn)行(′_`)排???序。

舉例:

postgres=# create table student(id serial,name character varying,course character varying,(′_`)score integer);
CREATE TABLE
postgres=#
postgres=# \d student
Table "public.student"
Column | Type | Modifiers
--------+-------------------+----------------------------------------------
id | integer | not null default nextval('student_id_seq'::regclass)
name | character varying |
course | characteヾ(′ω`)?r varying |
score | integer |

insert into student (name,course,score) values('周潤發(fā)','語(yǔ)文',89);
insert into student (name,course,score) values('??周潤發(fā)','數學(xué)',99);
insert into?? student (name,course,score) values('周潤發(fā)','外語(yǔ)',67);
in(╥_╥)sert into st(╥_╥)udent (name,cour??se,score) values('周潤發(fā)','物理',77);
insert into student (name,course,score) values('周潤發(fā)','化學(xué)',87);
insert into student (name,course,score) values('周星馳',(?⊿?)'語(yǔ)文',91);
insert into student (name,course,score) values('周??星馳','數學(xué)',81);(???)
insert into student (name,course,score) values('周星馳','外語(yǔ)',88);
insert into student (name,course,score) values('周星馳','物理',68);
insert into student (name,course,score) values('周星馳','化學(xué)',83);
in??sert into student (name,course,score) values('??黎明','語(yǔ)文',85??);
insert into(′ω`) student (name,course,score) values('黎明','數學(xué)',65);
insert into student (name,course,score) values('黎明','外語(yǔ)',95);
inser(′?_?`)t into student (name,course,s(╥_╥)core) values('黎明','物理',90);
in(′ω`)sert into student (name,course??,s??cor(°□°)e) values('黎明','化學(xué)',78);

1. 根據分數(′?`)排序

postgres???=# select *,row_nヾ(′▽?zhuān)??umber()?? over(order by score desc)rn from?? student;
id | name | course | score | rn
----+--------+--------+-------+----
2 | 周潤發(fā) | 數學(xué)┐(′д`)┌ | 99 | 1
13 | 黎明 | 外語(yǔ) | 95 | 2
6 | 周星馳 | 語(yǔ)文 | 91 | 3
14 | 黎明 | 物理 | 90 | 4
1 | 周潤發(fā) | 語(yǔ)文 | 89 | 5
8 | 周星馳 | 外語(yǔ) | 88 | 6
5 | 周潤發(fā) | 化學(xué) | 87 | 7
11 | 黎明 | 語(yǔ)文 | 85 | 8
10 | 周星馳 | 化學(xué) | 83 | 9
7 | 周星馳 | 數學(xué) | 81 | 10
15 | 黎明 | 化學(xué) | 78 | 11
4 | 周潤發(fā) | 物理 | 77 | 12
9 | 周星馳 | 物理 | 68 | 13
3 | 周潤發(fā) | 外語(yǔ) | 67 | 14
12 | 黎明 | 數學(xué) | 65 | 15
(15 rows)

rn是給我們的一個(gè)排序。

2. 根據科目分組,按分數排序

postgres=# select *,row_number() over(partition by course order by score desc)rn from student;
id | name | course | score | rn
--┐(′д`)┌--+--------+--------+-------+----
5 | 周潤發(fā) | 化學(xué) | 87 | 1
10 | 周星馳 | 化學(xué) | 83 | 2
15 | 黎明 | 化學(xué) | 78 | 3
13 | 黎明 | 外語(yǔ) | 95 | 1
8 | 周星馳 | 外語(yǔ) | 88 | 2
3 | 周潤發(fā) | 外語(yǔ) | 67 | 3
2 | 周潤發(fā) | 數學(xué) | 99 | 1
7 | 周星馳 | 數學(xué) | 81 | 2
12 | 黎明 | 數學(xué) | 65 | 3
14 | 黎明 | 物理 | 90 | 1
4 | 周潤發(fā) | 物理 | 77 | 2
9 | 周星馳 | 物理 | 68 | 3
6 | 周星??馳 | 語(yǔ)文 | 91ヽ(′▽?zhuān)?/ | 1
1 | 周潤發(fā) | 語(yǔ)文 | 89 | 2
11 | 黎明 | 語(yǔ)文 | 85 | 3
(15 rows)

3. 獲取每個(gè)科目的最高分

postgres=# selec??t * from(select *,row_number() over(partition by course order by score desc)rn from student)t where rn=1;
id | name | course | score | rn
----+--------+--------+-------+----
5 | 周潤發(fā) | 化學(xué) | 87 | 1
13 | 黎明 | 外語(yǔ) | 95 | 1
2 | 周潤發(fā) | 數學(xué) | 99 | 1
14 | 黎明 | 物理 | 90 | 1
6 | 周星馳 | 語(yǔ)文 | 91 | 1
(5 rows)

4. 每個(gè)科目的最低分也是一樣的

postgres=# select * from(°□°)(select *,row_number() over(partition by course order by score)rn from student)t where rn=1;
id | name | course | score | rn
----+--------+--------+-------+----
15 | 黎明 | 化學(xué) | 78?? | 1
3 | 周潤發(fā) | 外語(yǔ) | 67 | 1
12 | 黎明 | 數學(xué) | 65 | 1
9 | 周星馳 | 物理 | 68 | 1
11 | 黎明 | 語(yǔ)文 | 85 | 1
(5 rows)

只要在根據科目排序的時(shí)候按低到高順序排列就好了。

文章來(lái)源:腳本之家

來(lái)源地址:https://ww(′?_?`)w.jb51(?????).net/article/204797.htm

  1. 上一篇:運營(yíng)去哪里學(xué)比較好_運營(yíng)網(wǎng)站哪個(gè)好學(xué)_2
  2. 下一篇:沒(méi)有了;

其他產(chǎn)品

一本色道久久综合狠狠躁篇|亚洲av无码一区二区乱子伦as|亚洲国产成AV人天堂无码|亚洲狠狠婷婷综合久久蜜芽|狠狠做五月深爱婷婷|人妻夜夜爽天天爽三区 乌兰察布市| 秭归县| 南投市| 乃东县| 衡阳县| 梅河口市| 基隆市| 双峰县| 阿城市| 永善县| 江阴市| 拉萨市| 天台县| 新龙县| 三河市| 禹州市| 仁布县| 扶余县| 纳雍县| 山东| 杭锦旗| 灵璧县| 莱州市| 咸丰县| 南宁市| 岑巩县| 景泰县| 二手房| 永和县| 正定县| 斗六市| 舟曲县| 陇川县| 文昌市| 鄄城县| 青浦区| 车致| 安顺市| 宝丰县| 盖州市| 波密县| http://444 http://444 http://444 http://444 http://444 http://444