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

Routine to count unique values

Former Member
0 Likes
2,802

Hi Gurus,

I am BI user, I am trying to write a routine to populate count if employee and month from source fields is unique.

For example:

Employee calmonth count

10000 01.2008 1

10000 02.2008 1

10001 01.2008 1

10000 01.2008 0

Please can you help me with the code and any suggestions to write the code.

Regards,

Reddy.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,394

Hello Reddy

Here is some pseudo code

sort your_itab by empno and calmonth.

loop at your_itab into wa1

read another_itab with key empno calmonth (same struct as your_itab)

if found

wa-count = 0

modify your_itab from wa

else

wa -count = 1

append your_itab with wa

endif.

endloop

F1 will help with syntax.

Regards

Greg Kern

19 REPLIES 19
Read only

bpawanchand
Active Contributor
0 Likes
2,394
DATA :

  BEGIN OF fs_tab,
     empno(5) TYPE n,
     empdta TYPE sy-datum,
  END OF fs_tab.

data :
   wa_t type i.


DATA :
   t_tab LIKE
STANDARD TABLE
      OF fs_tab.


CLEAR fs_tab.
fs_tab-empno = '10000'.
fs_tab-empdta = '20081231'.
APPEND fs_tab TO t_tab.

CLEAR fs_tab.
fs_tab-empno = '10000'.
fs_tab-empdta = '20081231'.
APPEND fs_tab TO t_tab.

CLEAR fs_tab.
fs_tab-empno = '10001'.
fs_tab-empdta = '20081131'.
APPEND fs_tab TO t_tab.


DATA :
   wa_tab LIKE fs_tab.


LOOP AT t_tab INTO fs_tab.
IF wa_tab eq fs_tab.   " Comapring 
  write :
    / ' equal'.       
else.

wa_tab = fs_tab.

endif.

ENDLOOP.
Read only

Former Member
0 Likes
2,394

Your example is not sufficient, last record seems to be a bit confusing.

Can you explain with a good example in detail?

regards

Karthik D

Read only

0 Likes
2,394

Hi karthik,

for example the record is employee = 10000 cal month = 01.2008 the count should be 1.

If there is another record for employee =10000 cal month = 01.2008 the count should be 0.

that is for one particular employee for if there are 10 records for 01.2008 the should be one only.

Any help greatly appreciated. Please let me know if you still have any questions.

Thanks.

Best Regards,

Reddy.

Read only

0 Likes
2,394

So your requirement is to mark count with 0 if there exists already a record with the same combination, right?

If you are doing this for getting distinct values, you can try DELETE ADJACENT DUPLICATE...COMPARING statement and delete all repeating entries.

Regards

Karthik D

Read only

0 Likes
2,394

Hi Karthik,

As i don't have much knowledge in ABAP, Please can you give me example code. I will work on it. Thanks for reply.

Regards,

Reddy.

Read only

0 Likes
2,394

Hi Karthik,

I don't want to delete entries. I want to count employees monthly basis.

Cheers,

Reddy.

Read only

0 Likes
2,394

Hello,

If you have duplicate values in your internal table and want to delete the same then please SORT the table like.

sort itab by field1 field2. (this fields will be used to compare the duplicate values in your internal table)

then use.

delete adjacent duplicates from table itab.

Hope it helps.

Thanks,

Jayant

Read only

0 Likes
2,394

Hi reddy,

Again you are confusing me, you are telling that you want to count employees but in your example count should be 2 for the last record, right?

Regards

Karthik D

Read only

0 Likes
2,394

Hi karthik,

If the same employee has morethan one record in month the count should only be one. Thanks.

Regards,

Reddy.

Read only

Former Member
0 Likes
2,395

Hello Reddy

Here is some pseudo code

sort your_itab by empno and calmonth.

loop at your_itab into wa1

read another_itab with key empno calmonth (same struct as your_itab)

if found

wa-count = 0

modify your_itab from wa

else

wa -count = 1

append your_itab with wa

endif.

endloop

F1 will help with syntax.

Regards

Greg Kern

Read only

0 Likes
2,394

Hi Greg,

Thanks for reply, How to append the source values to itab. Please can you give me the code for appending the source values into internal table. Thanks.

Regards,

Reddy.

Read only

0 Likes
2,394

Hello Reddy.

A simple append

wa-val1 = 1

wa-val2 = 9

append wa to your_itab.

SAP help for APPEND will be more helpful. Use the F1 key.

Regards

Greg Kern

Read only

0 Likes
2,394

employee has to be the first, cal_month the second field in the internal table:

FIELD-SYMBOLS <line> TYPE ... . "has to be typed explicitly

SORT itab BY employee cal_month.

LOOP AT itab ASSIGNING <line>.

<line>-count = '0'.

AT NEW cal_month.

<line>-count = '1'.

ENDAT.

ENDLOOP.

Read only

0 Likes
2,394

Hi Eric,

I haven't understood

FIELD-SYMBOLS <line> TYPE ... . "has to be typed explicitly

Please can you explain in detail please. what is "line". Do i need to append source to internal table. I will paste my code in another reply. thanks.

Best Regards,

Reddy.

Read only

0 Likes
2,394

it has to be typed like a work area to your internal table. How is your internal table defined?

Read only

0 Likes
2,394

Hi Eric,

Please find below my code,

Data:

BEGIN of itab,

PERSONAL_NO(8) type n,

ZZMONTH(7) type c,

end of itab.

sort itab by PERSONAL_NO ZZMONTH.

loop at itab assigning line.

<line>-count = '0'.

AT NEW ZZMONTH.

<line>-count = '1'.

END AT.

endloop.

I haven't explicitly defined the line. can you give me the code for explicit defination.

Cheers,

Reddy.

Read only

0 Likes
2,394

I modified and completed your code:

TYPES : BEGIN OF ty_itab,
          personal_no(8) TYPE n,
          zzmonth(7)     TYPE c,
          count(1)       TYPE c,
        END OF ty_itab.
DATA : itab TYPE TABLE OF ty_itab.
FIELD-SYMBOLS : <line> TYPE ty_itab.

* here you need to fill some data
SORT itab BY personal_no zzmonth.

LOOP AT itab ASSIGNING <line>.
  <line>-count = '0'.
  AT NEW zzmonth.
    <line>-count = '1'.
  ENDAT.
ENDLOOP.

Read only

0 Likes
2,394

Hi Eric,

Below is my code, count is always showing "0".

----


  • CLASS routine IMPLEMENTATION

----


*

----


CLASS lcl_transform IMPLEMENTATION.

METHOD compute_0HDCNT_LAST.

  • IMPORTING

  • request type rsrequest

  • datapackid type rsdatapid

  • SOURCE_FIELDS-PERSONAL_NO TYPE N LENGTH 000008

  • SOURCE_FIELDS-ZZMONTH TYPE C LENGTH 000007

  • EXPORTING

  • RESULT type tys_TG_1-HDCNT_LAST

DATA:

MONITOR_REC TYPE rsmonitor.

$$ begin of routine - insert your code only below this line -

TYPES:

BEGIN of ty_itab,

PERSONAL_NO(8) type n,

ZZMONTH(7) type c,

count(1) type c,

end of ty_itab.

data: itab type table of ty_itab.

field-symbols : <line> type ty_itab.

append SOURCE_FIELDS-PERSONAL_NO to itab.

append SOURCE_FIELDS-ZZMONTH to itab.

sort itab by PERSONAL_NO ZZMONTH.

loop at itab assigning <line>.

<line>-count = '0'.

AT NEW ZZMONTH.

<line>-count = '1'.

ENDAT.

endloop.

$$ end of routine - insert your code only before this line -

ENDMETHOD. "compute_0HDCNT_LAST

----


  • Method invert_0HDCNT_LAST

----


*

  • This subroutine needs to be implemented only for direct access

  • (for better performance) and for the Report/Report Interface

  • (drill through).

  • The inverse routine should transform a projection and

  • a selection for the target to a projection and a selection

  • for the source, respectively.

  • If the implementation remains empty all fields are filled and

  • all values are selected.

*

----


*

----


METHOD invert_0HDCNT_LAST.

$$ begin of inverse routine - insert your code only below this line-

... "insert your code here

$$ end of inverse routine - insert your code only before this line -

ENDMETHOD. "invert_0HDCNT_LAST

ENDCLASS. "routine IMPLEMENTATION

Read only

0 Likes
2,394

can you check (in debug) what happens when these lines run:

append SOURCE_FIELDS-PERSONAL_NO to itab.

append SOURCE_FIELDS-ZZMONTH to itab.

I guess you'll have two lines in the internal table, but with not proper data.

Add the following line to the data declaration:

DATA : gw_itaby TYPE ty_itab.

and replace the above two lines with:

gw_itab-personal_no = SOURCE_FIELDS-PERSONAL_NO.

gw_itab-zzmonth = SOURCE_FIELDS-ZZMONTH .

APPEND itab.

APPEND itab.

(APPEND is twice, it is not a mistake)