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

table records issue

Former Member
0 Likes
1,218

Hi experts,

I have an issue with records in a table.

I want to know if i can solve it with an Abap logic.

The idea is if for the same date i have 2 records with the same key and differents status , then copy this record and assign a third status.

FYI, i will do it in a datasource user exit for BW.

Is this possible?

Thanks.

Amine

1 ACCEPTED SOLUTION
Read only

adam_krawczyk1
Contributor
0 Likes
1,186

Hi Amine,

I am not sure if you are referring to database or internal table for rows processing. With assumption that this is internal table, here is code example that creates rows to et_result_table for situations when rows with same counter value have different statuses:

TYPES BEGIN OF lty_status_s.

    TYPES counter TYPE i.

    TYPES status TYPE i.

TYPES END OF lty_status_s.


TYPES lty_status_tt TYPE TABLE OF lty_status_s.

  METHODS find_diff_statuses_same_key

      IMPORTING

        it_source_table TYPE lty_status_tt

      EXPORTING

        et_result_table TYPE lty_status_tt.

METHOD find_diff_statuses_same_key.


  DATA lt_sorted_table TYPE lty_status_tt.

  DATA ls_sorted_table TYPE lty_status_s.

  DATA ls_new_status_row TYPE lty_status_s.

  DATA l_last_counter TYPE I.


  lt_sorted_table = it_source_table.

  SORT lt_sorted_table ASCENDING BY counter status.

  LOOP AT lt_sorted_table INTO ls_sorted_table.

    AT NEW status.

 

      IF ( l_last_counter = ls_sorted_table-counter ).

        ls_new_status_row = ls_sorted_table.       

        ls_new_status_row-status = 10. " set new status here

        APPEND ls_new_status_row TO et_result_table.

      ENDIF.

 

    ENDAT.


  l_last_counter = ls_sorted_table-counter.


  ENDLOOP.


ENDMETHOD.

First I sorted table by counter and status. Then I am processing rows one by one. In case if there is new status found, I compare counter. If counter value is same as for last row, it means that we have situation as you described - for same counter, two different statuses. Then we copy the row to new table and update status to new (example 10). For simplification I used only counter comparison, but you can add also condition that last date must be equal as well as last counter.

I do not know exactly your requirements, if you can have more than one situation of same counter and many different statuses, or duplicated rows etc. This algorithm shows power of AT NEW statement that is triggered when sorted table finds next different value for column. If you want to extend algorithm you can also use AT NEW counter, or AT END OF status, AT END OF counter. AT END OF is triggered when loop is processing last same value for particular column. These statements are useful for algorithms with data duplications and may run common actions for same values in column.

Hope it helps.

Regards,

Adam

9 REPLIES 9
Read only

Former Member
0 Likes
1,186

hello,

yes you can. You need to loop then do a read for the entry with same key and date, copy it and change the status. You will need another table to store the new results though.

best regards,

swanand

Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
1,186

Hi Amine,

I don't understand how can two records have the same key.

Read only

Former Member
0 Likes
1,186

Hi Kumar,

Actually you are right.

It was a mistake from my part, it's the counter the key of my table.

What bother me, i am already doing a treatment in my user exit and now i have to combine the two of them.

IF s_data_cats-refcounter IS NOT INITIAL.

         SELECT SINGLE catshours FROM catsdb INTO wa_hours WHERE counter = s_data_cats-refcounter.

         IF sy-subrc = 0.

           s_data_cats-catshours = s_data_cats-catshours - wa_hours.

         ENDIF.

       ENDIF.

And now, if the ref counter is not initial

and the record containg the counter and the other one containing the refcounter have the same date, i have to create a record similair the ref counter but with another statut.

Thanks.

Amine

Read only

Former Member
0 Likes
1,186

Can we create an internal table in CMOD user exit for BW?

Thanks.

Amine

Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
1,186

Ok, so that should be simple now. Isn't it? I guess all you need to do is match the date for the two records (counter and reference counter) inside the IF block on sy-subrc. Also, you will need to modify your SELECT on catsdb so as to retrieve the entire record rather than just the catshours field.

Read only

Former Member
0 Likes
1,186

Yes in fact.

But i have another concern. All records must have a counter.

How can i manage the record that i am creating?

Thanks.

Amine

Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
1,186

That would depend on how the counter value is determined. If it's a sequential counter, you probably just need to increment the current value.

Read only

adam_krawczyk1
Contributor
0 Likes
1,187

Hi Amine,

I am not sure if you are referring to database or internal table for rows processing. With assumption that this is internal table, here is code example that creates rows to et_result_table for situations when rows with same counter value have different statuses:

TYPES BEGIN OF lty_status_s.

    TYPES counter TYPE i.

    TYPES status TYPE i.

TYPES END OF lty_status_s.


TYPES lty_status_tt TYPE TABLE OF lty_status_s.

  METHODS find_diff_statuses_same_key

      IMPORTING

        it_source_table TYPE lty_status_tt

      EXPORTING

        et_result_table TYPE lty_status_tt.

METHOD find_diff_statuses_same_key.


  DATA lt_sorted_table TYPE lty_status_tt.

  DATA ls_sorted_table TYPE lty_status_s.

  DATA ls_new_status_row TYPE lty_status_s.

  DATA l_last_counter TYPE I.


  lt_sorted_table = it_source_table.

  SORT lt_sorted_table ASCENDING BY counter status.

  LOOP AT lt_sorted_table INTO ls_sorted_table.

    AT NEW status.

 

      IF ( l_last_counter = ls_sorted_table-counter ).

        ls_new_status_row = ls_sorted_table.       

        ls_new_status_row-status = 10. " set new status here

        APPEND ls_new_status_row TO et_result_table.

      ENDIF.

 

    ENDAT.


  l_last_counter = ls_sorted_table-counter.


  ENDLOOP.


ENDMETHOD.

First I sorted table by counter and status. Then I am processing rows one by one. In case if there is new status found, I compare counter. If counter value is same as for last row, it means that we have situation as you described - for same counter, two different statuses. Then we copy the row to new table and update status to new (example 10). For simplification I used only counter comparison, but you can add also condition that last date must be equal as well as last counter.

I do not know exactly your requirements, if you can have more than one situation of same counter and many different statuses, or duplicated rows etc. This algorithm shows power of AT NEW statement that is triggered when sorted table finds next different value for column. If you want to extend algorithm you can also use AT NEW counter, or AT END OF status, AT END OF counter. AT END OF is triggered when loop is processing last same value for particular column. These statements are useful for algorithms with data duplications and may run common actions for same values in column.

Hope it helps.

Regards,

Adam

Read only

0 Likes
1,186

Hi Adam,

Thank you for the great explanations

Actually i never used the methods in Abap, i am working on BW side, but my aim is to improve my abap skills, it's very usefull when i am stuck especially at exctraction level when the bsuiness needs are complicated

I think that Kumar solution is more easy for me to implement and it takes in account the dates constraints.

Thanks.

Amine