我正在Greenplum DB中工作。Greenplum中的First_value窗口函数
First_value窗口函数得到奇怪的结果,当按顺序传递字符串值在所有行中都是通用的,那么它总是返回第一次插入的行,但理想情况下它应该返回任何值。下面是我的代码...
create temporary table test_first_value (id int,statename
varchar(50),episodeid int,
episodedate date) distributed by (id);
insert into test_first_value values(12,'MP',9863,'2015-11-06');
insert into test_first_value values(12,'MP',98123,'2009-11-06');
insert into test_first_value values(12,'MP',90123,'2017-03-06');
insert into test_first_value values(12,'MP',44567,'2013-03-17');
insert into test_first_value values(13,'MP',189300,'2013-03-17');
insert into test_first_value values(13,'MP',443467,'2016-03-19');
它总是返回其第一插入相同的值,该值是episodeid = 9863对ID = 12和episodeid = 189300为ID = 13
Select *,
First_value(episodeid) over(partition by id order by statename) as
first_episodeid,
First_value(episodedate) over(partition by id order by statename) as
first_episodedate
from
test_first_value;
现在,如果我改变我的插入顺序则总是返回这是第一次插入的行值是episodeid = 98123的ID = 12和episodeid = 443467为ID = 13
delete from test_first_value;
insert into test_first_value values(12,'MP',98123,'2009-11-06');
insert into test_first_value values(12,'MP',90123,'2017-03-06');
insert into test_first_value values(12,'MP',44567,'2013-03-17');
insert into test_first_value values(12,'MP',9863,'2015-11-06');
insert into test_first_value values(13,'MP',443467,'2016-03-19');
insert into test_first_value values(13,'MP',189300,'2013-03-17');
Select *,
First_value(episodeid) over(partition by id order by statename) as
first_episodeid,
First_value(episodedate) over(partition by id order by statename) as
first_episodedate
from
test_first_value;
请帮助我,我做错了。
编辑你的问题,并显示“奇怪的结果”。你也应该解释你希望得到的结果。 –
我认为你可能需要'按ID分区,按字母顺序排列状态名称' –
@Pra你只能按顺序使用状态名。所以你不能预测你在这种情况下会有什么。 –