I'm using ztable that has list of employees inside table, each employee has unique ID pernr
, however each change in database is reflected with duplicated IDs, therefore the table looks like this:
pernr | sname | begindate | enddate |
---|---|---|---|
1 | Name1 | 1.1.2000 | 1.1.2010 |
1 | Name1 | 2.1.2010 | 1.1.2015 |
1 | Name1 | 2.1.2015 | 31.12.9999 |
2 | Name2 | 1.1.2016 | 1.1.2019 |
2 | Name2 | 2.1.2019 | 31.12.9999 |
FORM process_data.
DATA: lt_ztable TYPE TABLE OF ztable,
ls_ztable LIKE LINE OF lt_ztable,
ls_salv_table LIKE LINE OF gt_salv_table.
SELECT pernr, sname, begindate, enddate
FROM ztable
INTO CORRESPONDING FIELDS OF TABLE lt_ztable.
LOOP AT lt_ztable INTO ls_ztable
MOVE-CORRESPONDING ls_ztable to ls_salv_table.
APPEND ls_salv_table TO gt_salv_table.
ENDLOOP.
ENDFORM.
What I need, is to filter out the data from ztable and display it as ALV cl_salv_table
through the gt_salv_table
however, only one record, the one with greatest difference between dates(so the ones ending with year 9999).
I was thinking do it within the SQL code, creating begindate, enddate AS datediff
, though I didn't know how to formulate the WHERE
statement that would filter only the records with greatest difference. Or would it be better to filter it out in the LOOP AT
part? Thanks for any idea.
You can do this by selecting data into internal table sorted with enddate in descending order and then delete the duplicates comparing pernr.
FORM process_data.
SELECT pernr, sname, begindate, enddate
FROM ztable
INTO TABLE gt_salv_table
**ORDER BY pernr ASCENDING enddate DESCENDING.**
IF sy-subrc EQ 0.
DELETE ADJACENT DUPLICATES FROM gt_salv_table COMPARING pernr.
ENDIF.
ENDFORM.
As futu suggested, MAX(enddate) can also be used, but you wont be able to select begindate in the aggregate query as the GROUP BY clause will need all not aggregated fields. If the business logic permits you could use MAX(begindate) and MAX(enddate) both to get all the data. Here, assumption is the name will be same for all records.
SELECT pernr, sname, MAX(begindate) AS begindate, MAX(enddate) AS enddate
FROM ztable
INTO TABLE @gt_salv_table
GROUP BY pernr, sname.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.