‎2018 Nov 29 8:19 AM
good morning
I was asked to count how many times the same record is presented in the ktokd field, using LOOP. I have no errors, but it does not end. Specifically, I started programming in a very short time.
TABLES: kna1.
TYPES:
BEGIN OF ty_out,
ktokd TYPE ktokd,
totale TYPE i,
END OF ty_out,
BEGIN OF ty_v,
ktokd TYPE ktokd,
END OF ty_v,
BEGIN OF ty_k,
ktokd TYPE ktokd,
totale TYPE i,
END OF ty_k.
DATA: it_k TYPE TABLE OF ty_k,
lwa_k LIKE LINE OF it_k,
it_out TYPE TABLE OF ty_out,
lwa_out LIKE LINE OF it_out,
it_v TYPE TABLE OF ty_v,
lwa_v LIKE LINE OF it_v.
SELECT ktokd
FROM kna1
INTO TABLE it_v.
CLEAR : lwa_k, lwa_out.
LOOP AT it_v INTO lwa_v.
REFRESH it_k.
SELECT COUNT(*) AS totale
FROM kna1
INTO CORRESPONDING FIELDS OF lwa_k
WHERE ktokd = lwa_v-ktokd.
lwa_out-ktokd = lwa_v-ktokd.
LOOP AT it_k INTO lwa_k.
lwa_out-totale = lwa_k-totale.
APPEND lwa_out TO it_out.
ENDLOOP.
ENDLOOP.
cl_demo_output=>display( it_out ).
‎2018 Nov 29 9:22 AM
first, you can count it easily with tcode TAANA so maybe you dont have to code.
second, i saw you already select all the ktokd into internal table so you just have to LOOP...GROUP BY and get the size of group which give you the answer. event without GROUP BY, i dont think you have to SELECT COUNT again inside LOOP, just loop and increase count variable is enough.
and i dont see you push data to it_k in any where...anw, i dont recommend your current code..
‎2018 Nov 29 10:07 AM
Also in your select to get the KTOKD values, you could have used a SELECT DISTINCT so the SELECT with the COUNT wouldn't be done multiple times for the same value of KTOKD.
‎2018 Nov 29 12:40 PM
Rob,
You can use ABAP, but Quynh is correct. If you want a quick, non-ABAP answer, then use transaction TAANA. Below is an example that I just did in an ECC 6.0 system. Follow these steps:
(1) Tcode TAANA
(2) Click "Start Table Analysis" (F8)
(3) Table Name = KNA1
(4) Click on the icon to the right of "Analysis Variant"
(5) Click on the "Create Ad Hoc Analysis Variant" button
(6) Move KTOKD from the Optional Fields to the Analysis Fields section
(7) Press ENTER
(8) Press ENTER again
(9) Select your processing option (Background or Online)
(10) Run
(11) Once done,double-click on KNA1 to see the results
