window_function ( expression, ... ) OVER (
PARTITION BY ...
ORDER BY ...
frame_clause
)
{ ROWS | RANGE } frame_start
{ ROWS | RANGE } BETWEEN frame_start AND frame_end
CREATE TABLE transfer_log
( log_id INTEGER NOT NULL PRIMARY KEY,
log_ts TIMESTAMP NOT NULL,
from_user VARCHAR(50) NOT NULL,
to_user VARCHAR(50),
type VARCHAR(10) NOT NULL,
amount NUMERIC(10) NOT NULL
);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (1,'2019-01-02 10:31:40','62221234567890',NULL,'存款',50000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (2,'2019-01-02 10:32:15','62221234567890',NULL,'存款',100000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (3,'2019-01-03 08:14:29','62221234567890','62226666666666','转账',200000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (4,'2019-01-05 13:55:38','62221234567890','62226666666666','转账',150000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (5,'2019-01-07 20:00:31','62221234567890','62227777777777','转账',300000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (6,'2019-01-09 17:28:07','62221234567890','62227777777777','转账',500000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (7,'2019-01-10 07:46:02','62221234567890','62227777777777','转账',100000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (8,'2019-01-11 09:36:53','62221234567890',NULL,'存款',40000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (9,'2019-01-12 07:10:01','62221234567890','62228888888881','转账',10000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (10,'2019-01-12 07:11:12','62221234567890','62228888888882','转账',8000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (11,'2019-01-12 07:12:36','62221234567890','62228888888883','转账',5000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (12,'2019-01-12 07:13:55','62221234567890','62228888888884','转账',6000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (13,'2019-01-12 07:14:24','62221234567890','62228888888885','转账',7000);
INSERT INTO transfer_log (log_id,log_ts,from_user,to_user,type,amount) VALUES (14,'2019-01-21 12:11:16','62221234567890','62228888888885','转账',70000);
select *
from (
select *,
sum(amount) over (partition by from_user order by log_ts range interval '5' day preceding) total_amount
from transfer_log
where type = '转账'
) t
where total_amount > 1000000;
log_id|log_ts |from_user |to_user |type|amount|total_amount|
------|-------------------|--------------|--------------|----|------|------------|
7|2019-01-10 07:46:02|62221234567890|62227777777777|转账 |100000| 1050000|
select *
from (
select *,
count(1) over (partition by from_user,to_user order by log_ts range interval '5' day preceding) times
from transfer_log
where type = '转账'
) t
where times >= 3;
log_id|log_ts |from_user |to_user |type|amount|times|
------|-------------------|--------------|--------------|----|------|-----|
7|2019-01-10 07:46:02|62221234567890|62227777777777|转账 |100000| 3|
select *, avg(amount) over (partition by product order by ym rows 4 preceding) next_amount
from sales_monthly
order by product,ym desc;
product |ym |amount |next_amount |
---------|------|--------|------------|
桔子 |201906|11524.00|11351.400000|
桔子 |201905|11423.00|11266.400000|
桔子 |201904|11327.00|11179.400000|
桔子 |201903|11302.00|11102.400000|
桔子 |201902|11181.00|11009.600000|
桔子 |201901|11099.00|10931.000000|
桔子 |201812|10988.00|10847.200000|
桔子 |201811|10942.00|10765.200000|
桔子 |201810|10838.00|10677.800000|
桔子 |201809|10788.00|10603.200000|
桔子 |201808|10680.00|10510.600000|
桔子 |201807|10578.00|10423.600000|
...
更多精彩推荐
☞好扑科技结合区块链行业发展趋势,重磅推出“好扑区块链合伙人”计划
点击阅读原文,精彩继续。
你点的每个“在看”,我都认真当成了喜欢