CODE1 CODE2 CODE3 RATE VALUE MONTH
A B C 1 1 202001
A B C 1 1 202002
A B C 1 1 202003
A B C 2 1 202004
A B C 2 1 202005
A B C 1 1 202006
A B C 1 1 202007
A B C 1 1 202008
A B C 1 1 202009
I am working on migrating data from old system to new system. As part of old system data maintained per month and will update the same row if data updated and table contains one row for a month I am migrating to news system and it contains start date and end date to make the active record . So on update new data needs to inserted and updated the old row end date
My expected data
CODE1 CODE2 CODE3 RATE VALUE START_DT END_DT
A B C 1 1 20200101 20200331
A B C 2 1 20200401 20200531
A B C 1 1 20200601 99991230
If the data active and we will update the date as infinity so 999912
But I am getting only two records and my query is below
CODE1 CODE2 CODE3 RATE VALUE START_DT END_DT
A B C 2 1 20200401 20200531
A B C 1 1 20200601 99991230
SELECT CODE1, CODE2, CODE3 RATE, VALUE,
TO_DATE(MIN(bus_month), 'yyyymm') AS START_DT,
last_day(TO_DATE(replace(MAX(bus_month), $CURRENTMONTG, '999912'), 'yyyymm')) AS end_date
FROM TEST_TABLE
GROUP BY CODE1, CODE2, CODE3, RATE, VALUE
SINCE I am grouping based on CODE1, CODE2, CODE3, RATE, VALUE and getting latest data based on grouping and I am not able to get the old data
Please help me to get the expected table structure. Thanks in advance
Please comment if any more details needed