3

I need to SELECT the 5 most recent notifications linked to an equipment but I also need to check that status DLFL (I0076 - deletion flag) is not active OR doesn't exist.

I think it should be pretty simple but I'm a bit confused with the fact that the status DLFL has 3 possible options.

  1. Exists in JEST table with inactive flag INITIAL.
  2. Exists in JEST table with inactive flag = "X". --> Valid record
  3. Does not exist in JEST table. --> Valid record

I only managed to get the last 5 by selecting all the history notifications and then removing the ones that have the status DLFL active like in the code below, but this means that I'm selecting hundreds of lines from last 20 years to only use 5.

How would you avoid this?

SELECT qmnum, erdat, stat FROM viqmel
  LEFT OUTER JOIN jest ON jest~objnr = viqmel~objnr
                      AND jest~inact = @abap_false
                      AND jest~stat  = 'I0076'
  WHERE viqmel~equnr = @equi "Input parameter
    AND viqmel~kzloesch = @abap_false
  ORDER BY erdat DESCENDING
  INTO TABLE @DATA(equi_notifs).

DATA equi_notifs_valid LIKE equi_notifs.
LOOP AT equi_notifs ASSIGNING FIELD-SYMBOL(<equi_notif>) WHERE stat IS INITIAL.
  equi_notifs_valid = VALUE #( BASE equi_notifs_valid ( <equi_notif> ) ).
  IF lines( equi_notifs_valid ) >= 5.
    EXIT.
  ENDIF.
ENDLOOP.
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
RaTiO
  • 979
  • 2
  • 17
  • 33

1 Answers1

1

Your second loop has no sense since you join only those JEST lines that has inactive I0076 status, making a logical error so equipment with no status I0076 is not caught at all in your dataset and your loop condition is never met.

The solution is to move status condition into WHERE and make filtering there.

Why not to use NOT EXISTS construction?

SELECT qmnum, erdat, stat FROM viqmel
  LEFT OUTER JOIN jest ON jest~objnr = viqmel~objnr
 WHERE viqmel~kzloesch = @abap_false
   AND ( jest~inact = @abap_true
   AND   jest~stat  = 'I0076' ) OR
  NOT EXISTS ( SELECT * FROM jest WHERE jest~objnr = viqmel~objnr AND jest~stat = 'I0076' )
  ORDER BY erdat DESCENDING
  INTO TABLE @DATA(equi_notifs)
  UP TO 5 ROWS.

Also addition UP TO N ROWS eliminates your ugly loop.

BTW, you mentioned you need only undeleted equipment lines, didn't you? Why you used abap_false with inact, probably it should be abap_true?

Jagger
  • 10,350
  • 9
  • 51
  • 93
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • Hi I think my 2nd loop makes sense, I tested it. My `SELECT` is a `LEFT OUTER JOIN` so the lines with `I0076` active will have a value on the `STAT` field, while the lines where status isn't found or is inactive will have no value. I tested it and it seems to work, getting one line even if there are multiple statuses. I already tried a solution like the one in your SELECT, but it's not working because JEST table can have other status, so it returns 1 line per each status different than `I0076` even for notifications that have `I0076` enabled. In my opinion my code works but it's not efficient – RaTiO Nov 12 '19 at 08:37
  • It depends on dataset, I cannot really test your case as my `qmel` table doesn't allow multiple notifs for the same `objnr` which is weird. Post your sample dataset to verify – Suncatcher Nov 12 '19 at 20:47
  • `QMEL/VIQMEL` only has 1 record per `objnr` but `JEST` table has multiple and when you do the `LEFT OUTER JOIN` + `NOT EXISTS`, then the `SELECT` returns a record for each `JEST` status different than `I0076`. That's why I ended up having to first select all records and then filtering if status is active, in order to only get the 5 valid that are more recent. – RaTiO Nov 13 '19 at 09:52
  • `QMEL/VIQMEL only has 1 record per objnr` then approach with date has no sense, you select not 5 last statuses for objnr, but 5 notifications for different objnrs which is nonsensical. I cannot imagine the use of such set – Suncatcher Nov 13 '19 at 19:29
  • Sorry, I guess I'm not explaining myself properly. But my sample `SELECT` is getting all the notifications linked to the equipment ID `@equi`. Then the `LOOP` takes only the 5 most recent with `DLFL` status not active. I would like to avoid first selecting all the linked notifications and directly get the 5 most recent that are valid, but I didn't manage yet. I don't want to select any status, I only need them for filtering, my expected final result is a valid list of 5 notifications IDs. – RaTiO Nov 13 '19 at 20:20