PostgreSQL simple select(group by and insert into ...select)

來源:互聯網
上載者:User

標籤:

 

warehouse_db=# create table student(number int primary key,name varchar(20),age int);
CREATE TABLE
warehouse_db=# insert into student values (1,‘David‘,20);
INSERT 0 1
warehouse_db=# insert into student values (2,‘Luna‘,21);
INSERT 0 1
warehouse_db=# insert into student values (3,‘Lisa‘,21);
INSERT 0 1
warehouse_db=# insert into student values (4,‘Susan‘,22);
INSERT 0 1
warehouse_db=# insert into student values (5,‘Jack‘,22);
INSERT 0 1
warehouse_db=# insert into student values (6,‘Peter‘,23);
INSERT 0 1
warehouse_db=# insert into student values (7,‘Andrew‘,23);
INSERT 0 1
warehouse_db=# insert into student values (8,‘Stallman‘,23);
INSERT 0 1

warehouse_db=# select * from student ;
number | name | age
--------+----------+-----
1 | David | 20
2 | Luna | 21
3 | Lisa | 21
4 | Susan | 22
5 | Jack | 22
6 | Peter | 23
7 | Andrew | 23
8 | Stallman | 23
(8 rows)
分組查詢
warehouse_db=# select age,count(*) from student group by age;
age | count
-----+-------
23 | 3
20 | 1
22 | 2
21 | 2
(4 rows)

warehouse_db=# select * from student order by age;
number | name | age
--------+----------+-----
1 | David | 20
2 | Luna | 21
3 | Lisa | 21
4 | Susan | 22
5 | Jack | 22
6 | Peter | 23
7 | Andrew | 23
8 | Stallman | 23
(8 rows)

warehouse_db=# select * from student order by age desc;
number | name | age
--------+----------+-----
8 | Stallman | 23
6 | Peter | 23
7 | Andrew | 23
5 | Jack | 22
4 | Susan | 22
3 | Lisa | 21
2 | Luna | 21
1 | David | 20
(8 rows)

warehouse_db=# select * from student where age <= 22 order by age desc;
number | name | age
--------+-------+-----
4 | Susan | 22
5 | Jack | 22
2 | Luna | 21
3 | Lisa | 21
1 | David | 20
(5 rows)

 

warehouse_db=# create table student_bak(number int primary key,name varchar(20),age int);
CREATE TABLE
warehouse_db=# insert into student_bak select * from student;
INSERT 0 8
warehouse_db=# select * from student_bak ;
number | name | age
--------+----------+-----
1 | David | 20
2 | Luna | 21
3 | Lisa | 21
4 | Susan | 22
5 | Jack | 22
6 | Peter | 23
7 | Andrew | 23
8 | Stallman | 23
(8 rows)

 

PostgreSQL simple select(group by and insert into ...select)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.