@@ -4,9 +4,9 @@ title: LAST_VALUE
44
55import FunctionDescription from '@site/src /components/FunctionDescription';
66
7- <FunctionDescription description =" ๆฐๅขๆๆดๆฐไบ ๏ผv1.2.697" />
7+ <FunctionDescription description =" ๅผๅ
ฅๆๆดๆฐไบ ๏ผv1.2.697" />
88
9- ่ฟๅ็ชๅฃๆกๆถ๏ผWindow Frame๏ผไธญ็ๆๅไธไธชๅผ ใ
9+ ่ฟๅ็ชๅฃๆกๆถไธญ็ๆๅไธไธชๅผ ใ
1010
1111ๅฆ่ฏทๅ้
๏ผ
1212
@@ -16,7 +16,7 @@ import FunctionDescription from '@site/src/components/FunctionDescription';
1616## ่ฏญๆณ
1717
1818``` sql
19- LAST_VALUE(expression)
19+ LAST_VALUE(expression) [ { RESPECT | IGNORE } NULLS ]
2020OVER (
2121 [ PARTITION BY partition_expression ]
2222 ORDER BY sort_expression [ ASC | DESC ]
@@ -26,56 +26,124 @@ OVER (
2626
2727** ๅๆฐ๏ผ**
2828- ` expression ` ๏ผๅฟ
้ใ่ฆ่ฟๅๆๅไธไธชๅผ็ๅๆ่กจ่พพๅผใ
29- - ` PARTITION BY ` ๏ผๅฏ้ใๅฐ่กๅๅไธบๅๅบใ
29+ - ` PARTITION BY ` ๏ผๅฏ้ใๅฐ่กๅๅไธบๅๅบ๏ผPartition๏ผ ใ
3030- ` ORDER BY ` ๏ผๅฟ
้ใ็กฎๅฎ็ชๅฃๅ
็ๆๅบๆนๅผใ
31- - ` window_frame ` ๏ผๅฏ้ใๅฎไน็ชๅฃๆกๆถ๏ผ้ป่ฎคไธบ๏ผ RANGE UNBOUNDED PRECEDING๏ผ ใ
31+ - ` window_frame ` ๏ผๅฏ้ใๅฎไน็ชๅฃๆกๆถใ้ป่ฎคๅผไธบ ` RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ` ใ
3232
33- ** ๆณจๆ ๏ผ**
33+ ** ่ฏดๆ ๏ผ**
3434- ่ฟๅๆๅบ็ชๅฃๆกๆถไธญ็ๆๅไธไธชๅผใ
35- - ๆฏๆ ` IGNORE NULLS ` ๅ ` RESPECT NULLS ` ้้กน ใ
36- - ้ๅธธ้่ฆๆพๅผๆๅฎ็ชๅฃๆกๆถๆ่ฝ่ทๅพ้ขๆ็ปๆ ใ
37- - ๅฏ็จไบๆฅๆพๆฏไธช็ปไธญ็ๆๆฐ/ๆ้ซๅผ ใ
35+ - ๆฏๆไฝฟ็จ ` IGNORE NULLS ` ่ทณ่ฟ็ฉบๅผ๏ผไฝฟ็จ ` RESPECT NULLS ` ไฟๆ้ป่ฎค่กไธบ ใ
36+ - ๅฝ้่ฆ่ทๅๅๅบ๏ผPartition๏ผ็็ๆญฃๆๅไธ่กๆถ๏ผ่ฏทไฝฟ็จๅจๅฝๅ่กไนๅ็ปๆ็ๆกๆถ๏ผไพๅฆ๏ผ ` ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ` ๏ผ ใ
37+ - ๅฏ็จไบๆฅๆพๆฏไธช็ปไธญ็ๆๆฐๅผ๏ผๆๅจๅๅ็็็ชๅฃไธญๆฅๆพๆ่ฟ็ๅผ ใ
3838
3939## ็คบไพ
4040
4141``` sql
42- -- ๅๅปบ็คบไพๆฐๆฎ
43- CREATE TABLE scores (
44- student VARCHAR (20 ),
45- score INT
42+ -- ็คบไพ่ฎขๅๆฐๆฎ
43+ CREATE OR REPLACE TABLE orders_window_demo (
44+ customer VARCHAR ,
45+ order_id INT ,
46+ order_time TIMESTAMP ,
47+ amount INT ,
48+ sales_rep VARCHAR
4649);
4750
48- INSERT INTO scores VALUES
49- (' Alice' , 95 ),
50- (' Bob' , 87 ),
51- (' Charlie' , 82 ),
52- (' David' , 78 ),
53- (' Eve' , 92 );
51+ INSERT INTO orders_window_demo VALUES
52+ (' Alice' , 1001 , to_timestamp(' 2024-05-01 09:00:00' ), 120 , ' Erin' ),
53+ (' Alice' , 1002 , to_timestamp(' 2024-05-01 11:00:00' ), 135 , NULL ),
54+ (' Alice' , 1003 , to_timestamp(' 2024-05-02 14:30:00' ), 125 , ' Glen' ),
55+ (' Bob' , 1004 , to_timestamp(' 2024-05-01 08:30:00' ), 90 , NULL ),
56+ (' Bob' , 1005 , to_timestamp(' 2024-05-01 20:15:00' ), 105 , ' Kai' ),
57+ (' Bob' , 1006 , to_timestamp(' 2024-05-03 10:00:00' ), 95 , NULL ),
58+ (' Carol' , 1007 , to_timestamp(' 2024-05-04 09:45:00' ), 80 , ' Lily' );
5459```
5560
56- ** ่ทๅๆไฝๅ๏ผๆๅๆฐ้ๅบๆๅๆถ็ๆๅไธไธชๅผ๏ผ๏ผ **
61+ ** ็คบไพ 1๏ผๆฏไธชๅฎขๆทๅๅบไธญ็ๆๆฐ่ฎขๅ **
5762
5863``` sql
59- SELECT student, score,
60- LAST_VALUE(score) OVER (
61- ORDER BY score DESC
62- ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
63- ) AS lowest_score,
64- LAST_VALUE(student) OVER (
65- ORDER BY score DESC
66- ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
67- ) AS lowest_student
68- FROM scores
69- ORDER BY score DESC ;
64+ SELECT customer,
65+ order_id,
66+ order_time,
67+ LAST_VALUE(order_id) OVER (
68+ PARTITION BY customer
69+ ORDER BY order_time
70+ ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
71+ ) AS last_order_for_customer
72+ FROM orders_window_demo
73+ ORDER BY customer, order_time;
7074```
7175
7276็ปๆ๏ผ
7377```
74- student | score | lowest_score | lowest_student
75- --------+-------+--------------+---------------
76- Alice | 95 | 78 | David
77- Eve | 92 | 78 | David
78- Bob | 87 | 78 | David
79- Charlie | 82 | 78 | David
80- David | 78 | 78 | David
78+ customer | order_id | order_time | last_order_for_customer
79+ ---------+----------+----------------------+-------------------------
80+ Alice | 1001 | 2024-05-01 09:00:00 | 1003
81+ Alice | 1002 | 2024-05-01 11:00:00 | 1003
82+ Alice | 1003 | 2024-05-02 14:30:00 | 1003
83+ Bob | 1004 | 2024-05-01 08:30:00 | 1006
84+ Bob | 1005 | 2024-05-01 20:15:00 | 1006
85+ Bob | 1006 | 2024-05-03 10:00:00 | 1006
86+ Carol | 1007 | 2024-05-04 09:45:00 | 1007
87+ ```
88+
89+ ** ็คบไพ 2๏ผๅจๆฏไธชๅฎขๆทๅ
ๅๅๆฅ็ 12 ๅฐๆถ**
90+
91+ ``` sql
92+ SELECT customer,
93+ order_id,
94+ order_time,
95+ amount,
96+ LAST_VALUE(amount) OVER (
97+ PARTITION BY customer
98+ ORDER BY order_time
99+ RANGE BETWEEN CURRENT ROW AND INTERVAL 12 HOUR FOLLOWING
100+ ) AS last_amount_next_12h
101+ FROM orders_window_demo
102+ ORDER BY customer, order_time;
103+ ```
104+
105+ ็ปๆ๏ผ
106+ ```
107+ customer | order_id | order_time | amount | last_amount_next_12h
108+ ---------+----------+----------------------+--------+----------------------
109+ Alice | 1001 | 2024-05-01 09:00:00 | 120 | 135
110+ Alice | 1002 | 2024-05-01 11:00:00 | 135 | 135
111+ Alice | 1003 | 2024-05-02 14:30:00 | 125 | 125
112+ Bob | 1004 | 2024-05-01 08:30:00 | 90 | 105
113+ Bob | 1005 | 2024-05-01 20:15:00 | 105 | 105
114+ Bob | 1006 | 2024-05-03 10:00:00 | 95 | 95
115+ Carol | 1007 | 2024-05-04 09:45:00 | 80 | 80
116+ ```
117+
118+ ** ็คบไพ 3๏ผๅๅๆซๆๆๅไธไธช้ๅฎไปฃ่กจๆถ่ทณ่ฟ็ฉบๅผ**
119+
120+ ``` sql
121+ SELECT customer,
122+ order_id,
123+ sales_rep,
124+ LAST_VALUE(sales_rep) RESPECT NULLS OVER (
125+ PARTITION BY customer
126+ ORDER BY order_time
127+ ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
128+ ) AS last_rep_respect,
129+ LAST_VALUE(sales_rep) IGNORE NULLS OVER (
130+ PARTITION BY customer
131+ ORDER BY order_time
132+ ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
133+ ) AS last_rep_ignore
134+ FROM orders_window_demo
135+ ORDER BY customer, order_id;
136+ ```
137+
138+ ็ปๆ๏ผ
139+ ```
140+ customer | order_id | sales_rep | last_rep_respect | last_rep_ignore
141+ ---------+----------+-----------+------------------+-----------------
142+ Alice | 1001 | Erin | Glen | Glen
143+ Alice | 1002 | NULL | Glen | Glen
144+ Alice | 1003 | Glen | Glen | Glen
145+ Bob | 1004 | NULL | NULL | Kai
146+ Bob | 1005 | Kai | NULL | Kai
147+ Bob | 1006 | NULL | NULL | Kai
148+ Carol | 1007 | Lily | Lily | Lily
81149```
0 commit comments