简体   繁体   English

根据最新的记录过滤掉z表中的记录

[英]Filter out records within z table according to the newest record

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:我正在使用 ztable 表中的员工列表,每个员工都有唯一的 ID pernr ,但是数据库中的每个更改都用重复的 ID 反映,因此该表如下所示:

pernr佩恩 sname名字 begindate开始日期 enddate结束日期
1 1 Name1姓名1 1.1.2000 1.1.2000 1.1.2010 1.1.2010
1 1 Name1姓名1 2.1.2010 2.1.2010 1.1.2015 1.1.2015
1 1 Name1姓名1 2.1.2015 2.1.2015 31.12.9999 31.12.9999
2 2 Name2姓名2 1.1.2016 1.1.2016 1.1.2019 1.1.2019
2 2 Name2姓名2 2.1.2019 2.1.2019 31.12.9999 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).我需要什么,是从ztable筛选出的数据,并显示为ALV cl_salv_table通过gt_salv_table然而,只有一个记录,一个具有最大差异日期之间(因此与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.我正在考虑在 SQL 代码中执行此操作,创建begindate, enddate AS datediff ,尽管我不知道如何制定WHERE语句来仅过滤差异最大的记录。 Or would it be better to filter it out in the LOOP AT part?还是在LOOP AT部分过滤掉它会更好? 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.您可以通过将数据选择到以 enddate 降序排序的内部表中,然后删除比较 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.正如 futu 所建议的,也可以使用 MAX(enddate),但您将无法在聚合查询中选择 begindate,因为 GROUP BY 子句将需要所有未聚合的字段。 If the business logic permits you could use MAX(begindate) and MAX(enddate) both to get all the data.如果业务逻辑允许,您可以使用 MAX(begindate) 和 MAX(enddate) 来获取所有数据。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM