oracle sql 问题,前面之间的行
oracle sql question, row between preceding
我需要帮助来理解 Oracle SQL 查询的以下部分。 BETWEEN 7 PRECEDING 和 7 PRECEDING 有什么作用?我了解,如果每个帐户回购一次以上,则车辆可以有多个回购日期。但是,这段代码正在像这样构建回购日期 1-7,我不确定它到底在做什么。如果有人可以解释一下,非常感谢。谢谢
, MIN(D0AL1.CONFIRM_DATE) OVER (PARTITION BY D0AL2.ACCOUNT_NBR
ORDER BY D0AL1.ASSIGNMENT_DATE ROWS BETWEEN 7 PRECEDING AND 7 PRECEDING) AS repo_date1
, MIN(D0AL1.CONFIRM_DATE) OVER (PARTITION BY D0AL2.ACCOUNT_NBR
ORDER BY D0AL1.ASSIGNMENT_DATE ROWS BETWEEN 6 PRECEDING AND 6 PRECEDING) AS repo_date2
, MIN(D0AL1.CONFIRM_DATE) OVER (PARTITION BY D0AL2.ACCOUNT_NBR
ORDER BY D0AL1.ASSIGNMENT_DATE ROWS BETWEEN 5 PRECEDING AND 5 PRECEDING) AS repo_date3
Analytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions in that they return multiple rows for each group. The group of rows is called a window ...
和
ROWS
| RANGE
These keywords define for each row a window (a physical or logical set of rows) used for calculating the function result. The function is then applied to all the rows in the window. The window moves through the query result set or partition from top to bottom.
ROWS
specifies the window in physical units (rows).
RANGE
specifies the window as a logical offset.
...
value_expr PRECEDING
or value_expr FOLLOWING
For RANGE or
ROW`:
If value_expr FOLLOWING
is the start point, then the end point must be value_expr FOLLOWING
.
If value_expr PRECEDING
is the end point, then the start point must be value_expr PRECEDING
.
您的每个子句在前后都使用相同的 value_expr,因此 window 被限制为恰好 1 行;回顾第一行 7 行,第二行 6 行,依此类推
作为生成内容的演示:
with t (id, dt) as (
select level, date '2018-01-01' + (level * 3)
from dual
connect by level <= 10
)
select id, dt,
min(id) over (order by dt rows between 7 preceding and 7 preceding) as a,
min(id) over (order by dt rows between 6 preceding and 6 preceding) as b,
min(id) over (order by dt rows between 5 preceding and 5 preceding) as c
from t
order by dt;
ID DT A B C
---------- ---------- ---------- ---------- ----------
1 2018-01-04
2 2018-01-07
3 2018-01-10
4 2018-01-13
5 2018-01-16
6 2018-01-19 1
7 2018-01-22 1 2
8 2018-01-25 1 2 3
9 2018-01-28 2 3 4
10 2018-01-31 3 4 5
生成的a、b、c列分别回溯7、6、5行,寻找要使用的值。如果没有那么远的匹配行,则结果为空。
另请注意,分析子句按日期值排序,这些日期不连续 - 但返回的 ID 是连续的。那是因为它按顺序查看行,而不是它们包含的实际值。如果您改用范围 window:
select id, dt,
min(id) over (order by dt range between 7 preceding and 7 preceding) as a,
min(id) over (order by dt range between 6 preceding and 6 preceding) as b,
min(id) over (order by dt range between 5 preceding and 5 preceding) as c
from t
order by dt;
ID DT A B C
---------- ---------- ---------- ---------- ----------
1 2018-01-04
2 2018-01-07
3 2018-01-10 1
4 2018-01-13 2
5 2018-01-16 3
6 2018-01-19 4
7 2018-01-22 5
8 2018-01-25 6
9 2018-01-28 7
10 2018-01-31 8
... 结果会大不相同,您只会看到日期 值 是 5、6 和 7 天前的 ID - 当我制作所有CTE 中有 3 天的间隔,结果列 b
中只有匹配项。 None 行有 5 天或 7 天前的任何内容,它们只有 6 天前的行(作为 3 的倍数),并且前几行仍然没有那么远的匹配项。如果您将此扩展到 3 天前,您也会看到这些匹配项。
我需要帮助来理解 Oracle SQL 查询的以下部分。 BETWEEN 7 PRECEDING 和 7 PRECEDING 有什么作用?我了解,如果每个帐户回购一次以上,则车辆可以有多个回购日期。但是,这段代码正在像这样构建回购日期 1-7,我不确定它到底在做什么。如果有人可以解释一下,非常感谢。谢谢
, MIN(D0AL1.CONFIRM_DATE) OVER (PARTITION BY D0AL2.ACCOUNT_NBR
ORDER BY D0AL1.ASSIGNMENT_DATE ROWS BETWEEN 7 PRECEDING AND 7 PRECEDING) AS repo_date1
, MIN(D0AL1.CONFIRM_DATE) OVER (PARTITION BY D0AL2.ACCOUNT_NBR
ORDER BY D0AL1.ASSIGNMENT_DATE ROWS BETWEEN 6 PRECEDING AND 6 PRECEDING) AS repo_date2
, MIN(D0AL1.CONFIRM_DATE) OVER (PARTITION BY D0AL2.ACCOUNT_NBR
ORDER BY D0AL1.ASSIGNMENT_DATE ROWS BETWEEN 5 PRECEDING AND 5 PRECEDING) AS repo_date3
Analytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions in that they return multiple rows for each group. The group of rows is called a window ...
和
ROWS
|RANGE
These keywords define for each row a window (a physical or logical set of rows) used for calculating the function result. The function is then applied to all the rows in the window. The window moves through the query result set or partition from top to bottom.
ROWS
specifies the window in physical units (rows).
RANGE
specifies the window as a logical offset....
value_expr
PRECEDING
or value_exprFOLLOWING
For
RANGE or
ROW`:
If value_expr
FOLLOWING
is the start point, then the end point must be value_exprFOLLOWING
.If value_expr
PRECEDING
is the end point, then the start point must be value_exprPRECEDING
.
您的每个子句在前后都使用相同的 value_expr,因此 window 被限制为恰好 1 行;回顾第一行 7 行,第二行 6 行,依此类推
作为生成内容的演示:
with t (id, dt) as (
select level, date '2018-01-01' + (level * 3)
from dual
connect by level <= 10
)
select id, dt,
min(id) over (order by dt rows between 7 preceding and 7 preceding) as a,
min(id) over (order by dt rows between 6 preceding and 6 preceding) as b,
min(id) over (order by dt rows between 5 preceding and 5 preceding) as c
from t
order by dt;
ID DT A B C
---------- ---------- ---------- ---------- ----------
1 2018-01-04
2 2018-01-07
3 2018-01-10
4 2018-01-13
5 2018-01-16
6 2018-01-19 1
7 2018-01-22 1 2
8 2018-01-25 1 2 3
9 2018-01-28 2 3 4
10 2018-01-31 3 4 5
生成的a、b、c列分别回溯7、6、5行,寻找要使用的值。如果没有那么远的匹配行,则结果为空。
另请注意,分析子句按日期值排序,这些日期不连续 - 但返回的 ID 是连续的。那是因为它按顺序查看行,而不是它们包含的实际值。如果您改用范围 window:
select id, dt,
min(id) over (order by dt range between 7 preceding and 7 preceding) as a,
min(id) over (order by dt range between 6 preceding and 6 preceding) as b,
min(id) over (order by dt range between 5 preceding and 5 preceding) as c
from t
order by dt;
ID DT A B C
---------- ---------- ---------- ---------- ----------
1 2018-01-04
2 2018-01-07
3 2018-01-10 1
4 2018-01-13 2
5 2018-01-16 3
6 2018-01-19 4
7 2018-01-22 5
8 2018-01-25 6
9 2018-01-28 7
10 2018-01-31 8
... 结果会大不相同,您只会看到日期 值 是 5、6 和 7 天前的 ID - 当我制作所有CTE 中有 3 天的间隔,结果列 b
中只有匹配项。 None 行有 5 天或 7 天前的任何内容,它们只有 6 天前的行(作为 3 的倍数),并且前几行仍然没有那么远的匹配项。如果您将此扩展到 3 天前,您也会看到这些匹配项。