带窗口子句的高级 sql
Advanced sql with windowing claus
SELECT a.*,
SUM(s.amount) over(ORDER BY s.month rows unbounded preceding) AS a ,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows unbounded preceding) AS b,
SUM(s.amount) over(PARTITION BY s.month ) AS c_1,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND unbounded following) AS c,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN 1 preceding AND unbounded following) AS d,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN 1 preceding AND 1 following) AS e,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND 1 following) AS f,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows CURRENT ROW) AS g
FROM all_sales s,
(SELECT *
FROM all_sales) a
WHERE s.rowid = a.rowid;
/
--上面的查询给出下面显示的结果 c_1 和 c 列之间的区别。
YEAR MONTH PRD_TYPE_ID EMP_ID AMOUNT A B C_1 C D E F G
1 2006 1 1 21 1.00 1 1 10 10 10 3 3 1
2 2006 1 1 21 2.00 3 3 10 10 10 6 6 2
3 2005 1 2 21 3.00 6 6 10 10 9 9 10 3
4 2005 1 2 22 4.00 10 10 10 10 7 7 10 4
5 2006 2 1 21 5.00 15 5 11 11 11 11 11 5
6 2005 2 1 21 6.00 21 11 11 11 11 11 11 6
7 2005 3 1 21 21 7 7 7 7 7
8 2006 3 2 21 7.00 28 7 7 7 7 7 7 7
9 2005 4 1 21 8.00 36 8 17 17 17 17 17 8
10 2006 4 2 21 9.00 45 17 17 17 17 17 17 9
11 2006 5 2 21 45 10 10 10 10 10
12 2005 5 1 21 10.00 55 10 10 10 10 10 10 10
13 2006 6 1 21 11.00 66 11 23 23 23 23 23 11
14 2005 6 1 21 12.00 78 23 23 23 23 23 23 12
15 2005 7 2 21 13.00 91 13 27 27 27 27 27 13
16 2006 7 1 21 14.00 105 27 27 27 27 27 27 14
17 2005 8 2 21 15.00 120 15 31 31 31 31 31 15
18 2006 8 1 21 16.00 136 31 31 31 31 31 31 16
19 2005 9 2 21 17.00 153 17 35 35 35 35 35 17
20 2006 9 1 21 18.00 171 35 35 35 35 35 35 18
21 2005 10 2 21 19.00 190 19 39 39 39 39 39 19
22 2006 10 1 21 20.00 210 39 39 39 39 39 39 20
23 2006 11 1 21 21.00 231 21 43 43 43 43 43 21
24 2005 11 1 21 22.00 253 43 43 43 43 43 43 22
25 2006 12 2 21 23.00 276 23 47 47 47 47 47 23
26 2005 12 1 21 24.00 300 47 47 47 47 47 47 24
你得到了相同的结果,因为你的陈述完全相同:
SUM(s.amount) over(PARTITION BY s.month ) AS c_1,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND unbounded following) AS c
原因是条件BETWEEN unbounded preceding AND unbounded following
,因为它覆盖了整个分区范围。
来自 MSDN:
- UNBOUNDED PRECEDING - Specifies that the window starts at the first row of the partition. UNBOUNDED PRECEDING can only be specified as window starting point.
- UNBOUNDED FOLLOWING - Specifies that the window ends at the last row of the partition. UNBOUNDED FOLLOWING can only be specified as a window end point. For example RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING defines a window that starts with the current row and ends with the last row of the partition.
它们加起来等于 PARTITION BY s.month
SELECT a.*, SUM(s.amount) over(ORDER BY s.month rows unbounded preceding) AS a , SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows unbounded preceding) AS b, SUM(s.amount) over(PARTITION BY s.month ) AS c_1, SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND unbounded following) AS c, SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN 1 preceding AND unbounded following) AS d, SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN 1 preceding AND 1 following) AS e, SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND 1 following) AS f, SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows CURRENT ROW) AS g FROM all_sales s, (SELECT * FROM all_sales) a WHERE s.rowid = a.rowid;
/ --上面的查询给出下面显示的结果 c_1 和 c 列之间的区别。
YEAR MONTH PRD_TYPE_ID EMP_ID AMOUNT A B C_1 C D E F G 1 2006 1 1 21 1.00 1 1 10 10 10 3 3 1 2 2006 1 1 21 2.00 3 3 10 10 10 6 6 2 3 2005 1 2 21 3.00 6 6 10 10 9 9 10 3 4 2005 1 2 22 4.00 10 10 10 10 7 7 10 4 5 2006 2 1 21 5.00 15 5 11 11 11 11 11 5 6 2005 2 1 21 6.00 21 11 11 11 11 11 11 6 7 2005 3 1 21 21 7 7 7 7 7 8 2006 3 2 21 7.00 28 7 7 7 7 7 7 7 9 2005 4 1 21 8.00 36 8 17 17 17 17 17 8 10 2006 4 2 21 9.00 45 17 17 17 17 17 17 9 11 2006 5 2 21 45 10 10 10 10 10 12 2005 5 1 21 10.00 55 10 10 10 10 10 10 10 13 2006 6 1 21 11.00 66 11 23 23 23 23 23 11 14 2005 6 1 21 12.00 78 23 23 23 23 23 23 12 15 2005 7 2 21 13.00 91 13 27 27 27 27 27 13 16 2006 7 1 21 14.00 105 27 27 27 27 27 27 14 17 2005 8 2 21 15.00 120 15 31 31 31 31 31 15 18 2006 8 1 21 16.00 136 31 31 31 31 31 31 16 19 2005 9 2 21 17.00 153 17 35 35 35 35 35 17 20 2006 9 1 21 18.00 171 35 35 35 35 35 35 18 21 2005 10 2 21 19.00 190 19 39 39 39 39 39 19 22 2006 10 1 21 20.00 210 39 39 39 39 39 39 20 23 2006 11 1 21 21.00 231 21 43 43 43 43 43 21 24 2005 11 1 21 22.00 253 43 43 43 43 43 43 22 25 2006 12 2 21 23.00 276 23 47 47 47 47 47 23 26 2005 12 1 21 24.00 300 47 47 47 47 47 47 24
你得到了相同的结果,因为你的陈述完全相同:
SUM(s.amount) over(PARTITION BY s.month ) AS c_1,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND unbounded following) AS c
原因是条件BETWEEN unbounded preceding AND unbounded following
,因为它覆盖了整个分区范围。
来自 MSDN:
- UNBOUNDED PRECEDING - Specifies that the window starts at the first row of the partition. UNBOUNDED PRECEDING can only be specified as window starting point.
- UNBOUNDED FOLLOWING - Specifies that the window ends at the last row of the partition. UNBOUNDED FOLLOWING can only be specified as a window end point. For example RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING defines a window that starts with the current row and ends with the last row of the partition.
它们加起来等于 PARTITION BY s.month