‎2008 Nov 06 1:01 PM
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.
‎2008 Nov 06 3:19 PM
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
‎2008 Nov 06 1:20 PM
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.
‎2008 Nov 06 1:20 PM
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
‎2008 Nov 06 1:25 PM
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.
‎2008 Nov 06 1:39 PM
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
‎2008 Nov 06 2:06 PM
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.
‎2008 Nov 06 2:07 PM
Hi Karthik,
I don't want to delete entries. I want to count employees monthly basis.
Cheers,
Reddy.
‎2008 Nov 06 2:08 PM
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
‎2008 Nov 06 2:28 PM
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
‎2008 Nov 06 3:07 PM
Hi karthik,
If the same employee has morethan one record in month the count should only be one. Thanks.
Regards,
Reddy.
‎2008 Nov 06 3:19 PM
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
‎2008 Nov 06 3:28 PM
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.
‎2008 Nov 06 3:43 PM
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
‎2008 Nov 06 3:58 PM
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.
‎2008 Nov 07 10:13 AM
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.
‎2008 Nov 07 10:15 AM
it has to be typed like a work area to your internal table. How is your internal table defined?
‎2008 Nov 07 10:38 AM
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.
‎2008 Nov 07 10:44 AM
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.
‎2008 Nov 07 11:19 AM
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
‎2008 Nov 07 12:16 PM
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)