Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SELECT Major record

f_allocca
Participant
0 Likes
567

in a ZTAB, for the same ZFIELD1 field that is not a key field, I have to take the record with the higher ZFIELD2, how can I do it?

1 ACCEPTED SOLUTION
Read only

BiberM
Contributor
501

For a single select you can filter for ZFIELD1 and then order by ZFIELD2 descending:

select *
from ztab
where zfield1 eq 'ABC' order by zfield2 descending
into @data(select_result_single).
endselect.

For a mass selection you can group by ZFIELD1 and select the maximum ZFIELD2 if you are only interested in these fields:

select zfield1,
max(zfield2)
from ztab
group by zfield1
into table @data(select_result_multiple).

If you need the whole line you will have to use above select as preselection and select the whole record afterwards (best: join, also possible: sub-select). This could the also be hidden inside cds-views.
This is often needed when needing to select the most up to data record for every key combination.

For a single select you can filter for ZFIELD1 and then order by ZFIELD2 descending:

select *
from ztab
where zfield1 eq 'ABC' order by zfield2 descending
into @data(select_result_single).
endselect.

For a mass selection you can group by ZFIELD1 and select the maximum ZFIELD2 if you are only interested in these fields:

select zfield1,
max(zfield2)
from ztab
group by zfield1
into table @data(select_result_multiple).

If you need the whole line you will have to use above select as preselection and select the whole record afterwards (best: join, also possible: sub-select). This could the also be hidden inside cds-views.
This is often needed when needing to select the most up to data record for every key combination.

1 REPLY 1
Read only

BiberM
Contributor
502

For a single select you can filter for ZFIELD1 and then order by ZFIELD2 descending:

select *
from ztab
where zfield1 eq 'ABC' order by zfield2 descending
into @data(select_result_single).
endselect.

For a mass selection you can group by ZFIELD1 and select the maximum ZFIELD2 if you are only interested in these fields:

select zfield1,
max(zfield2)
from ztab
group by zfield1
into table @data(select_result_multiple).

If you need the whole line you will have to use above select as preselection and select the whole record afterwards (best: join, also possible: sub-select). This could the also be hidden inside cds-views.
This is often needed when needing to select the most up to data record for every key combination.