By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table my_table(Id int, Serial_Number int, Last_update_date date);
insert into my_table values
(1, 3344, '2024-05-07')
,(2, 3344, '2024-05-02')
,(3, 9098, '2024-02-20')
,(4, 9098, '2024-02-20')
,(5, 2323, '2023-03-07')
,(6, 2323, '2023-10-17')
,(7, 2323, '2023-10-17')
,(8, 1894, '2022-07-10')
,(9, 1894, '2024-02-20')
,(10, 1894, '2024-05-02')
;
select * from my_table;
Id | Serial_Number | Last_update_date |
---|---|---|
1 | 3344 | 2024-05-07 |
2 | 3344 | 2024-05-02 |
3 | 9098 | 2024-02-20 |
4 | 9098 | 2024-02-20 |
5 | 2323 | 2023-03-07 |
6 | 2323 | 2023-10-17 |
7 | 2323 | 2023-10-17 |
8 | 1894 | 2022-07-10 |
9 | 1894 | 2024-02-20 |
10 | 1894 | 2024-05-02 |
with cte as (
select a.*
,dense_rank()over (partition by serial_number order by last_update_date) as rnk
from my_table a
)
select *
,case when max(rnk)over(partition by serial_number)<count(*)over(partition by serial_number)
then 'Y'
else 'N'
end flag
from cte
order by id
Id | Serial_Number | Last_update_date | rnk | flag |
---|---|---|---|---|
1 | 3344 | 2024-05-07 | 2 | N |
2 | 3344 | 2024-05-02 | 1 | N |
3 | 9098 | 2024-02-20 | 1 | Y |
4 | 9098 | 2024-02-20 | 1 | Y |
5 | 2323 | 2023-03-07 | 1 | Y |
6 | 2323 | 2023-10-17 | 2 | Y |
7 | 2323 | 2023-10-17 | 2 | Y |
8 | 1894 | 2022-07-10 | 1 | N |
9 | 1894 | 2024-02-20 | 2 | N |
10 | 1894 | 2024-05-02 | 3 | N |