注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
以下都是我自己总结归纳的,都加上了reference link。
先在电脑上安装psql,这样你可以测试下面的code
这个链接可以教你在mac上安装,或者直接google。https://medium.com/@Umesh_Kafle/ ... mac-os-87fa98a6814d
[b]
. 1point3acres.com
首先为什么我们要使用window functions?[/b]
[reference: https://codingsight.com/grouping-data-using-the-over-and-partition-by-functions/]
-baidu 1point3acres
先看一个例子,假设我们有这样一个table
name | gender | age | total_score | id
--------+--------+-----+-------------+----
Jolly | Female | 20 | 500 | 1-baidu 1point3acres
Jon | Male | 22 | 545 | 2
Sara | Female | 25 | 600 | 3
Laura | Female | 18 | 400 | 4
Alan | Male | 20 | 500 | 5
Kate | Female | 22 | 500 | 6
Joseph | Male | 18 | 643 | 7
Mice | Male | 23 | 543 | 8
Wise | Male | 21 | 499 | 9
. 1point3acres.com Elis | Female | 27 | 400 | 10
. Χ
你可以自己在psql创建这个table
CREATE TABLE student ( name VARCHAR(50) NOT NULL, gender VARCHAR(50) NOT NULL, age INT NOT NULL, total_score INT NOT NULL ); INSERT INTO student VALUES ('Jolly', 'Female', 20, 500), ('Jon', 'Male', 22, 545), ('Sara', 'Female', 25, 600), ('Laura', 'Female', 18, 400), ('Alan', 'Male', 20, 500), ('Kate', 'Female', 22, 500), ('Joseph', 'Male', 18, 643), ('Mice', 'Male', 23, 543), ('Wise', 'Male', 21, 499), ('Elis', 'Female', 27, 400);
. check 1point3acres for more.
.google и
. From 1point 3acres bbs
然后我们想得到下面这样的table
name | gender | total_students | average_age | total_score
--------+--------+----------------+---------------------+-------------.1point3acres
Elis | Female | 5 | 22.4000000000000000 | 2400.1point3acres
Sara | Female | 5 | 22.4000000000000000 | 2400
Laura | Female | 5 | 22.4000000000000000 | 2400.1point3acres
Kate | Female | 5 | 22.4000000000000000 | 2400 ..
Jolly | Female | 5 | 22.4000000000000000 | 2400
Wise | Male | 5 | 20.8000000000000000 | 2730
Jon | Male | 5 | 20.8000000000000000 | 2730
Joseph | Male | 5 | 20.8000000000000000 | 2730
Mice | Male | 5 | 20.8000000000000000 | 2730
Alan | Male | 5 | 20.8000000000000000 | 2730
.google и
这个时候可以看到行数没有改变,但是增加了三个总结的columns all partitioned by the gender
if we do it through aggregation functions
SELECT gender, count(gender) AS Total_Students, round(AVG(age)::numeric, 2) as Average_Age, SUM(total_score) as Total_Score FROM student GROUP BY gender
This would only return two rows, one row for female and another and we would have to join the table back
SELECT id, name, Aggregation.gender, Aggregation.Total_students, Aggregation.Average_Age, Aggregation.Total_Score FROM student INNER JOIN (SELECT gender, count(gender) AS Total_students, round(AVG(age)::numeric, 2) AS Average_Age, SUM(total_score) AS Total_Score FROM student GROUP BY gender) AS Aggregation on Aggregation.gender = student.gender ORDER BY gender, name;
An easy way to do it is through window functions
SELECT id, name, gender, COUNT(gender) OVER (PARTITION BY gender) AS Total_students, ROUND(AVG(age) OVER (PARTITION BY gender)::numeric, 2) AS Average_Age, SUM(total_score) OVER (PARTITION BY gender) AS Total_Score FROM student; . 1point 3 acres
window functions 最好的好处是,不会改变行数!!!!. 1point3acres.com
再看一个leetocde的SQL: Highest Salary in Departments
Classic Highest Salary in Departments
![]()
Leetcode MySQL Solution
SELECT
Department.name AS 'Department',. Χ
Employee.name AS 'Employee',
Salary
. 1point 3 acresFROM
Employee.1point3acres
JOIN
Department ON Employee.DepartmentId = Department.Id
WHERE
(Employee.DepartmentId , Salary) IN
( SELECT
DepartmentId, MAX(Salary)
FROM
Employee-baidu 1point3acres
GROUP BY DepartmentId
) ..
; | Department | Employee | Salary |
|------------|----------|--------|
| Sales | Henry | 80000 |
| IT | Max | 90000 |
Postgresql with Window Functions
SELECT *
. ----FROM (
SELECT
last_name,
salary,
department,
rank() OVER (
PARTITION BY department
ORDER BY salary
DESC
) as rank
FROM employees) sub_query
WHERE rank = 1;
last_name salary department rank
Jones 45000 Accounting 1
Smith 55000 Sales 1
Johnson 40000 Marketing 1
.1point3acres
如果有人看我再继续写下面的 :P 这个文本编辑器有点点难用。。。
什么时候可以不用Partitioned By 和 Order By,分别的影响是?
ROW_NUMBER, RANK, DENSE_RANK?
LEAD, LAG?. Waral dи,
What is Rolling SUM, Rolling Average?
Example on Monthy Over Month Retention, Growth
|