PostgreSQL ROW_NUMBER()OVER()の使い方説明
5759 ワード
構文:
ROW_NUMBER()は戻りの記録のために行番号を定義し、PATION BY col 1はcol 1グループによるもので、ORDER BY col 2[DESC]はcol 2によるソートです。
例:
2.科目によってグループ化し、点数順に並べ替えます。
追加:SQL:postgresqlでは、お問い合わせの結果に自己増加シーケンスのROW_を追加します。NUMBER()OVER()の使用
例を挙げて説明します
検索文は言いません。select...from...where
GROUTP BYの役割:
このコードの実行結果は、
GROUTP BYを削除すると、実行結果は以下の通りです。
同じstarttimeデータが二つ確認されました。
これにより、
GROUTP BYの役割は、分類のまとめです。つまり、アンケート結果の中で、starttimeは一つしかありません。
GROUTP BYの役割:
DESCを変えたら
対照的に,ORDER BYの役割は並べ替えを行うためであることが分かった。
ある要求に従って固定的に並べ替えます。
加えて
明らかに対照的に、私達は最終的な調査結果のために増加したidシーケンスを一列追加しました。(ここでidは改名できます。「id」は他のものに変えてもいいです。)
これにより結論が得られ、実行にはrow_があるということです。number()over()「xx」のSQL文の場合、コードは先にクエリ文を実行し、over中のコマンドを実行し、最後に結果のために列の自己増加シーケンスを追加します。
以上は個人の経験ですので、参考にしていただければと思います。間違いがあったり、完全に考えていないところがあれば、教えてください。
ROW_NUMBER() OVER( [ PRITITION BY col1] ORDER BY col2[ DESC ] )
説明:ROW_NUMBER()は戻りの記録のために行番号を定義し、PATION BY col 1はcol 1グループによるもので、ORDER BY col 2[DESC]はcol 2によるソートです。
例:
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 | character varying |
score | integer |
insert into student (name,course,score) values(' ',' ',89);
insert into student (name,course,score) values(' ',' ',99);
insert into student (name,course,score) values(' ',' ',67);
insert into student (name,course,score) values(' ',' ',77);
insert into student (name,course,score) values(' ',' ',87);
insert into student (name,course,score) values(' ',' ',91);
insert into student (name,course,score) values(' ',' ',81);
insert into student (name,course,score) values(' ',' ',88);
insert into student (name,course,score) values(' ',' ',68);
insert into student (name,course,score) values(' ',' ',83);
insert into student (name,course,score) values(' ',' ',85);
insert into student (name,course,score) values(' ',' ',65);
insert into student (name,course,score) values(' ',' ',95);
insert into student (name,course,score) values(' ',' ',90);
insert into student (name,course,score) values(' ',' ',78);
1.スコアによる並べ替え
postgres=# select *,row_number() over(order by score desc)rn from student;
id | name | course | score | rn
----+--------+--------+-------+----
2 | | | 99 | 1
13 | | | 95 | 2
6 | | | 91 | 3
14 | | | 90 | 4
1 | | | 89 | 5
8 | | | 88 | 6
5 | | | 87 | 7
11 | | | 85 | 8
10 | | | 83 | 9
7 | | | 81 | 10
15 | | | 78 | 11
4 | | | 77 | 12
9 | | | 68 | 13
3 | | | 67 | 14
12 | | | 65 | 15
(15 rows)
rnは私たちに与えられた順序です。2.科目によってグループ化し、点数順に並べ替えます。
postgres=# select *,row_number() over(partition by course order by score desc)rn from student;
id | name | course | score | rn
----+--------+--------+-------+----
5 | | | 87 | 1
10 | | | 83 | 2
15 | | | 78 | 3
13 | | | 95 | 1
8 | | | 88 | 2
3 | | | 67 | 3
2 | | | 99 | 1
7 | | | 81 | 2
12 | | | 65 | 3
14 | | | 90 | 1
4 | | | 77 | 2
9 | | | 68 | 3
6 | | | 91 | 1
1 | | | 89 | 2
11 | | | 85 | 3
(15 rows)
3.各科目の最高得点を取得する
postgres=# select * 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 | | | 87 | 1
13 | | | 95 | 1
2 | | | 99 | 1
14 | | | 90 | 1
6 | | | 91 | 1
(5 rows)
4.各科目の最低点も同じです。
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 | | | 78 | 1
3 | | | 67 | 1
12 | | | 65 | 1
9 | | | 68 | 1
11 | | | 85 | 1
(5 rows)
科目別に並べるときは低い順に並べばいいです。追加:SQL:postgresqlでは、お問い合わせの結果に自己増加シーケンスのROW_を追加します。NUMBER()OVER()の使用
例を挙げて説明します
SELECT ROW_NUMBER
() OVER ( ORDER BY starttime DESC ) "id",
starttime AS "text",
starttime
FROM
warning_products
WHERE
pid_model = ' '
AND starttime IS NOT NULL
GROUP BY
starttime
このコードの中で:検索文は言いません。select...from...where
GROUTP BYの役割:
このコードの実行結果は、
GROUTP BYを削除すると、実行結果は以下の通りです。
同じstarttimeデータが二つ確認されました。
これにより、
GROUTP BYの役割は、分類のまとめです。つまり、アンケート結果の中で、starttimeは一つしかありません。
GROUTP BYの役割:
DESCを変えたら
() OVER ( ORDER BY starttime ASC ) "id",
実行結果は:対照的に,ORDER BYの役割は並べ替えを行うためであることが分かった。
ある要求に従って固定的に並べ替えます。
ROW_NUMBER () OVER() “id”
まず見てみます。この区間を削除したら、運行結果:加えて
明らかに対照的に、私達は最終的な調査結果のために増加したidシーケンスを一列追加しました。(ここでidは改名できます。「id」は他のものに変えてもいいです。)
これにより結論が得られ、実行にはrow_があるということです。number()over()「xx」のSQL文の場合、コードは先にクエリ文を実行し、over中のコマンドを実行し、最後に結果のために列の自己増加シーケンスを追加します。
以上は個人の経験ですので、参考にしていただければと思います。間違いがあったり、完全に考えていないところがあれば、教えてください。