- raw data
no | group | date | value | flag |
---|---|---|---|---|
1 | a | 2022-10-13 | old | y |
2 | a | 2022-10-15 | new | y |
3 | b | 2022-01-01 | old | n |
4 | b | 2022-01-03 | new | n |
- step1. insert no1 raw
- step2. modify date value using by no2 raw
and I want to update latest date no1 raw using by no2 raw
and the condition is where `flag` = "y"
- final sql table
no | group | date | value | flag |
---|---|---|---|---|
1 | a | 2022-10-15 | old | y |
3 | b | 2022-01-01 | old | n |
is it possible?
+) I insert/update raw data line by line.
2
Answers
Not entirely clear but I hope below answer gives you a hint if not the solution.
Above code will select the max(date) per group if the flag=Y otherwise it will take the date per row.
https://dbfiddle.uk/JhRUti2h
The solution is to self join the source table and select the right field, prioritizing the latest date.
Here you have a working query:
The result is what you expected: