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: 

Count of "Client-specific" record in the itab

former_member623629
Participant
0 Kudos
1,224

Hi all,

I am using the following code to get the row count of an Internal Table

  IF sy-subrc IS INITIAL.
    SELECT KUNNR VBELN FROM VBAK
      INTO TABLE g_it_vbak
      FOR ALL ENTRIES IN g_it_kna1
      WHERE KUNNR EQ g_it_kna1-kunnr.

    g_anzahl = lines( g_it_vbak ).

However, I am looking to get the number of row count against KUNNR. I mean I need to have output where I will have lines count per client and not the total number of row counts against the query.

Thanks in advance.

Arnab

1 ACCEPTED SOLUTION

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
1,067

Did you tried SELECT .. COUNT ?

-- Tomas --
8 REPLIES 8

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
1,068

Did you tried SELECT .. COUNT ?

-- Tomas --

0 Kudos
1,067

At first I did with COUNT( * ). This gave me the same result until and unless I put the Select inside a LOOP. The person who made the request for the program did not like Select inside a loop. So, I am trying to find another solution.

1,067

arnab19871 try something like this. You will get count of VBELN per each KUNNR in one SELECT statement. Study the documentation and examples for more details.
EDIT: you will have to replace FOR ALL ENTRIES IN with range table (WHERE kunnr IN lt_kunnr_range_tab), because COUNT does not support "FAE".

SELECT COUNT( DISTINCT vbeln ) kunnr<br>  INTO TABLE lt_orders_per_customer
-- Tomas --

1,067
Arnab Goswami I hope you have understood why it's not good to have a SELECT inside a LOOP 🙂

0 Kudos
1,067

sandra.rossi Yes, my boss explained.

1,067

You have a good boss!

1,067

se38 Yes, he is a great person. That's why he gave a middle age man with no coding experience a chance. I just hope I can learn fast and make him feel that he did not make any mistake.

former_member623629
Participant
0 Kudos
1,067

Thanks for the input guys. The problem is solved. I am attaching my code for anybody who might have the same problem in the future. Please see the comment section of this post for the full Programme.

Data Declaration:

Data: g_anzahl TYPE i.

Select Option:

IF sy-subrc IS INITIAL.
    SELECT KUNNR VBELN FROM VBAK
      INTO CORRESPONDING FIELDS OF TABLE g_it_vbak
      FOR ALL ENTRIES IN g_it_kna1
      WHERE KUNNR EQ g_it_kna1-kunnr.
  ENDIF.

To get the desired output: (This loop is placed inside another loop for the output)

    IF sy-subrc = 0.
      CLEAR g_anzahl.
      LOOP AT g_it_vbak INTO g_wa_vbak WHERE KUNNR = g_wa_kna1-kunnr.
        g_anzahl = g_anzahl + 1.
*     ADD 1 to g_anzahl.
      ENDLOOP.