I wanted to get records from a single table where end_date
column values are same of particular so_line_id
and state value. Due to that I do self join from table itself
id | so_line_id | field | value | end_date | create_date |
---|---|---|---|---|---|
1 | 4040 | product | submitted | 2022-02-07 02:03:40 | 2022-02-07 02:03:30 |
2 | 4040 | product | pending_fulfillment | 2022-02-07 03:03:10 | 2022-02-07 02:03:40 |
3 | 4040 | product | active | NULL | 2022-02-07 03:03:10 |
4 | 4040 | product | active | NULL | 2022-02-15 06:20:10 |
I tried to get result via query mention below
select * from product_history h1 join product_history h2 on h1.so_line_id = h2.so_line_id and h2.id != h1.id and h1.end_date = h2.end_date where h1.so_line_id = 4040;
Above completely works fine when we have datetime values instead of NULL.