Application Development 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: 

Get count of distinct values based on a output field

0 Kudos
237

Hi!

I am trying to get the count of similar items of a field in an internal table and put it in the counter column. I ahve the following code below but the result always shows zero in the entire column. Would really appreciate if someone can pls help me out on this.

What I want the output to show is :

Name  Address  Counter

A             A01       

B             A01        02

C             B01

D            B01

E             B01        03

F             C01

G            C01        02

H            D01        01

SORT c_t_data BY build.

DATA : lv_build
TYPE build
,

c_t_data1 TYPE TABLE OF ptdw_times WITH HEADER LINE,

wa_data1 TYPE ptdw_times,

v_flg TYPE c.

c_t_data1[] = c_t_data[].

LOOP AT c_t_data1 INTO wa_data1.

AT NEW build.

counter = counter
+
1.

ENDAT.

AT END OF build.

wa_data1-counter
= counter.

ENDAT.

ENDLOOP.

c_t_data[] = c_t_data1[].

ENDFORM. "execute_user_exit

Thanks

1 ACCEPTED SOLUTION

former_member209120
Active Contributor
0 Kudos
184

Hi Sumit Tiwari,

Try like this

types : begin of ty_final,
         a type string,
         b TYPE string,
         c type i,
         end of ty_final.

data : it_final type TABLE OF ty_final,
        wa_final type ty_final.

data : var type i,
        idx TYPE sy-tabix.


wa_final-a = 'A01'.
wa_final-b = 'A'.
append wa_final to it_final.
clear wa_final.

wa_final-a = 'A01'.
wa_final-b = 'B'.
append wa_final to it_final.
clear wa_final.

wa_final-a = 'B01'.
wa_final-b = 'C'.
append wa_final to it_final.
clear wa_final.

wa_final-a = 'B01'.
wa_final-b = 'D'.
append wa_final to it_final.
clear wa_final.

wa_final-a = 'B01'.
wa_final-b = 'E'.
append wa_final to it_final.
clear wa_final.


Loop at it_final into wa_final.
idx = sy-tabix.

  var = var + 1.

  at END OF a.
   wa_final-c = var.
   clear var.
modify it_final INDEX idx FROM wa_final TRANSPORTING c .
clear wa_final.
endat.
endloop.

Loop at it_final into wa_final.

write :/ wa_final-b, wa_final-a, wa_final-c.

endloop.


8 REPLIES 8

Former Member
0 Kudos
184

Please modify your internal table with the new counter using modify statement

0 Kudos
184

Hi Maju!

Thanks for the anser but even that does not bring in the counters.

                                                  Counter

  10000002 8.0 0.0 HR A01

  10000002 8.0 0.0 HR A01

  10000002 8.0 0.0 HR A01

  10000002 8.0 0.0 HR B01

  10000002 8.0 0.0 HR B01

  10000002 8.0 0.0 HR B01

  10000002 8.0 0.0 HR B01

  10000002 8.0 0.0 HR B01

  10000002 8.0 0.0 HR B01

  10000002 8.0 0.0 HR B01

  10000002 8.0 0.0 HR B01

  10000002 8.0 0.0 HR B01

  10000002 8.0 0.0 HR B01

LOOP AT c_t_data1 INTO wa_data1.
    AT NEW build.
     counter = counter + 1.
    ENDAT.
    AT END OF build.
      wa_data1-counter = counter.
    ENDAT.
    MODIFY c_t_data1 FROM wa_data1.
  ENDLOOP.
  c_t_data[] = c_t_data1[].

arindam_m
Active Contributor
0 Kudos
184

Hi,

Try the following logic should work.

LOOP AT c_t_data INTO wa_data.

"Set current index and index of one row above current
lv_curr_indx = sy-tabix - 1.

lv_prev_indx = sy-tabix - 1.


     AT NEW build.

       "Clear counter to reset and set the initial value

       CLEAR: counter.
       counter = counter + 1.
    ENDAT.

"Increment the count.

counter = counter + 1.

   AT END OF build.
      wa_data-counter = counter.

     MODIFY c_t_data FROM wa_data TRANSPORTING counter INDEX lv_prev_indx.

     CLEAR: counter.
   ENDAT.


ENDLOOP.

former_member209120
Active Contributor
0 Kudos
185

Hi Sumit Tiwari,

Try like this

types : begin of ty_final,
         a type string,
         b TYPE string,
         c type i,
         end of ty_final.

data : it_final type TABLE OF ty_final,
        wa_final type ty_final.

data : var type i,
        idx TYPE sy-tabix.


wa_final-a = 'A01'.
wa_final-b = 'A'.
append wa_final to it_final.
clear wa_final.

wa_final-a = 'A01'.
wa_final-b = 'B'.
append wa_final to it_final.
clear wa_final.

wa_final-a = 'B01'.
wa_final-b = 'C'.
append wa_final to it_final.
clear wa_final.

wa_final-a = 'B01'.
wa_final-b = 'D'.
append wa_final to it_final.
clear wa_final.

wa_final-a = 'B01'.
wa_final-b = 'E'.
append wa_final to it_final.
clear wa_final.


Loop at it_final into wa_final.
idx = sy-tabix.

  var = var + 1.

  at END OF a.
   wa_final-c = var.
   clear var.
modify it_final INDEX idx FROM wa_final TRANSPORTING c .
clear wa_final.
endat.
endloop.

Loop at it_final into wa_final.

write :/ wa_final-b, wa_final-a, wa_final-c.

endloop.


0 Kudos
184

Hi Ramesh,

Is there any way we could finish it in a single statement? I'm not sure, many aggregate functions are not available i guess. Like using count(column_nm) with distinct? Or else something on that line because its really a task to loop and would be better if we could finish such things in the select query itself. Is it possible?

Regards,

KS

0 Kudos
184

Hi Kushboo,

I think it is not possible to do count in select statement itself.

0 Kudos
184

Yeah, i gave my half a day some time back to somehow do it in select statement itself. Then i left it but its kind of sad that these are not possible in select query itself and i wonder why, because internal table concept is really good but one always wants an independent way out.

Anyways thanks a lot for your reply,

KS

0 Kudos
184

Thank You...