【发布时间】:2020-08-18 17:22:25
【问题描述】:
表:
num value category name
503 8978 bird woodpecker
502 7812 animal
502 7812 animal
501 7812 animal
500 7812 animal panther
499 7812 animal
498 7812 animal
467 7812 animal elephant
在value 和category 列的partition by 内,应创建输出列,如下所示:
当name 不为null 时,output 列占用name 的值,并在num 列的加2 和减2 范围内填充相同的值。
例如,500 有name not null,500-2=498 和 500+2=502,在 498 到 502 的范围内,输出填充为panther
输出:
num value category name output
503 8978 bird woodpecker
502 7812 animal panther
502 7812 animal panther
501 7812 animal panther
500 7812 animal panther panther
499 7812 animal panther
498 7812 animal panther
467 7812 animal elephant elephant
【问题讨论】:
-
您需要解决的另外几个问题。为什么 501 和 502 不包含“啄木鸟”?它们也落在名称不为空的行的 +/- 2 内。这个选择背后的逻辑是什么?当 name 在 2 个或多个连续 num 值中不为空时会发生什么?如果 502 行中的 1 行包含非空名称但另一行不包含,或者任何时候 2 行仅在名称上不同而其中一个为空,会发生什么情况?
-
为什么 501 和 502 不包含“啄木鸟”? -----
Because it's based on partition by value and category,如果 name 在 2 个或多个连续 num 值中不为 null 会发生什么? ---output would be null,如果 502 行中的 1 行包含非空名称但另一行没有,会发生什么情况,-----output is null when name is not null,谢谢@Belayer -
pc_pyr,你试试查询吗?
-
(1) 为什么
num列中有重复项? (2) 你用的是什么数据库? -
select version();给你什么?
标签: sql postgresql window-functions