:)
I use postgresql 13 and I was sure I understand how to use PARTITION BY
but I guess I was wrong. I created the following example to illustrate my problem.
I created this table:
id, group_id,order_id,text
1 1 1 a1
2 1 2 b1
3 2 1 a2
4 2 2 b2
5 3 1 a3
so you've got unique id, group_id, and order in each group, and a text.
I wanted to select each row with order_id 1, so I have the first line of each group, and to also fetch the text of the last order_id of each group.
so I ran this query:
select id,group_id,order_id,the_text,
last_value(the_text) over (partition by group_id order by order_id) last_text
from cdrs.my_test
where order_id=1;
and it returned
id,group_id,order_id,the_text,last_text
1,1,1,a1,a1
3,2,1,a2,a2
5,3,1,a3,a3
last_text and the_text are the same, it doesn't matter if I choose first_value()
or last_value()
.
the last_text column should have returned with b1, b2, a3.
I was sure that when I get a row, I can use partition by
to group that row with a rule, and get data from other rows in the group.
it really doesn't make sense to me why it doesn't work.
any information regarding this issue would be greatly appreciated.
Best Regards
Kfir