2023 Jun 21 10:45 AM
I wrote a select query to get the latest record:
SELECT SINGLE MAX( obknr ) MAX( datum ) INTO (wa_obknr, wa_datum) FROM objk WHERE equnr = lv_equip-ast_asset_num AND taser = 'SER01' GROUP BY obknr datum.
The above query should get the latest record as shown in the image attached
But its fetching 1st record only not the second record. I need to fetch 2nd record
2023 Jun 21 11:49 AM
AFAIK you cannot use aggregate function on the group by fields, remove the max, use a order by clause and select up to 1 rows.
2023 Jun 21 12:17 PM
You didn't explain with words what you want to do, so impossible to understand what you want to achieve.
For information, this code compiles (in ABAP 7.57):
SELECT SINGLE MAX( obknr ), MAX( datum )
FROM objk
WHERE equnr = '' AND taser = 'SER01'
INTO (@data(wa_obknr), @data(wa_datum)).
2023 Jun 21 1:52 PM
sandra.rossi The above code should have fetched value with date: 24.02.2023, but its fetching the value with date: 09.01.2023
2023 Jun 21 2:10 PM
2023 Jun 21 2:15 PM
I guess I understand what you mean, but it's your screenshot which is wrong and doesn't illustrate at all what you mean.
2023 Jun 21 2:58 PM
sandra.rossi So the solution is to not use GROUP BY keyword when we are using aggregate keywords( like : MAX).
Thank you Sandra.
2023 Jun 21 3:25 PM
Is it a generalization that you are proposing for all cases? If so, NOT AT ALL.
If I understand well what you mean (you still didn't clarify), you are right that you must not use GROUP BY (but not because you are using MAX). Use either Raymond solution, or use a window expression, or use IN ( SELECT ... MAX ... ), etc. (many possibilities as you can see)
I guess you won't clarify so it could be something like:
SELECT SINGLE obknr, datum
FROM objk
WHERE equnr = '000000000100001024' AND taser = 'SER03'
AND obknr IN ( SELECT MAX( obknr )
FROM objk AS objk_2
WHERE equnr = objk~equnr
AND taser = objk~taser )
INTO (@DATA(wa_obknr), @DATA(wa_datum)).