Former Member

# how to calculate no.of records for a specific company Code and Transaction

Hi All,

Could you please help me how to calculate or count no.of records, in a table which are having a specific Company Code and Transaction Code and populate in an output table displaying that these many records are present for this company code and Transaction code.

The table is having the data like this:

LOB TRAN CODE COUNT

X1 1

X1 2

X1 3

X1 4

F1 NB 5

F1 NB 6

F1 NB 7

F1 NB 8

F1 NB 9

F1 NB 10

F1 NB 11

F1 NB 12

F1 NB 13

F1 NB 14

F1 NB 15

F1 NB 16

F1 NB 17

F1 NB 18

F1 NB 19

F1 NB 20

F1 NB 21

F1 NB 22

F1 NB 23

F1 24

F1 25

F1 26

F1 27

In the above table there are totally 27 records,where as the no.of records with F1 as company code and NB as transaction code are 18..so i have to display like F1 NB and 18 as one record as output.

Thanks and Regards,

Johny

10|10000 characters needed characters exceeded

Former Member
Sep 04, 2008 at 05:12 AM

Hi Dude,

Data : V_count type i.

U loop at the internal table.

Inside the loop U write the READ Statement with key F1 as company code and NB as transaction code .

if sy-subrc is initial.

V_count = V_count + 1.

endif.

Endloop.

write : V_count.

Hope this solves UR Problem.

10|10000 characters needed characters exceeded
• Former Member
Sep 04, 2008 at 05:13 AM

DATA : COUNT TYPE I.

If it is database table write this below code.

SELECT LOB TRAN CODE COUNT

COUNT( * ) AS count

FROM zppt_tran_001

INTO CORRESPONDING FIELDS OF TABLE i_final

WHERE lob = 'F1'

GROUP BY LOB TRAN CODE COUNT.

If it is internal table

loop at i_final where lob = 'F1'.

count = count + 1.

endloop.

Edited by: murali papana on Sep 4, 2008 1:14 AM

10|10000 characters needed characters exceeded
• Former Member
Sep 04, 2008 at 05:15 AM

Hello

```data: counter type i.
loop at itab where lob = 'F1' and trancode = 'NB'.
counter = counter + 1.
endloop.
write: counter.
```

10|10000 characters needed characters exceeded
• Former Member
Sep 04, 2008 at 05:26 AM

HI Johny,

just paste this sample code in ABAP editor and eexcute...

```REPORT ZSBN_TEST_1 .

DATA : BEGIN OF ITAB OCCURS 0,
LOB(3),
TCODE(3),
CNT(6),
END OF ITAB.

DATA : ITAB_LOB   LIKE ITAB OCCURS 0 WITH HEADER LINE,
ITAB_TCODE LIKE ITAB OCCURS 0 WITH HEADER LINE,
ITAB_FIN   LIKE ITAB OCCURS 0 WITH HEADER LINE,
I(6).

ITAB-LOB = 'F1'.
ITAB-TCODE = 'N1'.

DO 10 TIMES.
ITAB-CNT = ITAB-CNT + 2.
APPEND ITAB.
ENDDO.

ITAB-LOB = 'F1'.
ITAB-TCODE = 'N2'.
DO 3 TIMES.
ITAB-CNT = ITAB-CNT + 2.
APPEND ITAB.
ENDDO.

WRITE : 'BEFORE PRCOESSING'.
LOOP AT ITAB.
WRITE :/ ITAB-LOB, ITAB-TCODE, ITAB-CNT.
ENDLOOP.

*&*& FRO LOB
ITAB_LOB[] = ITAB[].
SORT ITAB_LOB BY LOB.
DELETE ADJACENT DUPLICATES FROM ITAB_LOB COMPARING LOB.

*&*& FRO TCODE
ITAB_TCODE[] = ITAB[].
SORT ITAB_TCODE BY TCODE.
DELETE ADJACENT DUPLICATES FROM ITAB_TCODE COMPARING TCODE.

*&*& FOR COUNT
LOOP AT ITAB_LOB.
LOOP AT ITAB_TCODE.
LOOP AT ITAB WHERE LOB   EQ ITAB_LOB-LOB
AND  TCODE EQ ITAB_TCODE-TCODE.

I = I + 1.

ENDLOOP.
ITAB_FIN-LOB = ITAB_LOB-LOB.
ITAB_FIN-TCODE = ITAB_TCODE-TCODE.
ITAB_FIN-CNT = I.
APPEND ITAB_FIN.
CLEAR : ITAB_FIN, I.

ENDLOOP.
ENDLOOP.

SKIP 2.
WRITE :/ 'AFTER PRCOESSING'.
LOOP AT ITAB_FIN.
WRITE :/ ITAB_FIN-LOB, ITAB_FIN-TCODE, ITAB_FIN-CNT.
ENDLOOP.
```

With Rgds,

S.Bharani

10|10000 characters needed characters exceeded
• Former Member
Sep 04, 2008 at 05:15 AM

You can go for Control Breaks for your requirement.

First sort the internal table by company code and transaction. Declare a counter variable.

Now loop the internal table and increment the counter variable. within the loop use AT END OF tcode.......ENDAT. Within the control break append the counter variable as well as the company code and tcode to some other ITAB and clear the counter.

This will give you the total number of records for a combination of co code and tcodes.

This will be a good reference for your requirement. What I am doing here is, for each Vendor i am calculating total number of deliveries and amounts for that vendor. For every new vendor i am writing the ouput on to a list and clearing the counter variables.

```  LOOP AT t_vend_prod_grp INTO fs_vend_prod_grp.

w_total_delv = w_total_delv + 1.
w_total_amt = w_total_amt + fs_vend_prod_grp-dmbtr.

AT NEW matkl.

w_mat_grp = fs_vend_prod_grp-matkl.
* To display the material group and the header texts for the data
* grouped by material group and vendor.
SKIP 1.
INTENSIFIED,
/ w_mat_grp COLOR COL_NORMAL
INTENSIFIED OFF.
SKIP 1.

ULINE  1(54).
WRITE:/ sy-vline,
2 text-ven,
12 sy-vline,
text-dlv,
26 sy-vline,
38 text-amt,
sy-vline,
46 text-cur,
54 sy-vline.
ULINE /1(54).

ENDAT.                             " AT NEW MATKL

AT END OF lifnr.

* To display the actual data for the grouping based on Material group
* and Vendor.
FORMAT COLOR COL_NORMAL ON INTENSIFIED OFF.
WRITE: / sy-vline,
2 fs_vend_prod_grp-lifnr COLOR COL_KEY,
12 sy-vline,
w_total_delv,
26 sy-vline,
w_total_amt CURRENCY text-usd,
sy-vline,
46 text-usd,
54 sy-vline.
FORMAT COLOR COL_NORMAL OFF INTENSIFIED OFF.

CLEAR: w_total_amt,
w_total_delv.
ENDAT.                             " AT END OF LIFNR

ENDLOOP.                             " LOOP AT T_VEND_PROD_GRP

```

10|10000 characters needed characters exceeded
• Former Member
Sep 04, 2008 at 06:02 AM

Hi Every one,

Thank you very much for your swift reponse..

but here I cannnot know whether there are records with F1 as company code or not.

So,with out knowig how to do..the sum for all those records with one specific code and Transaction code.

Regards,

Johny