2014 Dec 17 11:34 AM
Hi All,
We have a requirement where we have fields Employee ,Plan ,Start date ,end date.
An employee can have more than 1 Plans in a year.
We only need to show those employees in report who have more than one Plan in a year.
Sample Data:
Employee Plan Start Date End Date
101 A 01.01.2014 31.03.2014
101 B 01.01.2014 31.07.2014
101 C 01.08.2014 31.12.2014
102 D 01.01.2014 30.04.2014
103 E 01.01.2014 31.12.2014
104 F 01.01.2014 30.06.2014
104 G 01.07.2014 31.12.2014
Expected Output:
Employee Plan Start Date End Date
101 A 01.01.2014 31.03.2014
101 B 01.01.2014 31.07.2014
101 C 01.08.2014 31.12.2014.
104 F 01.01.2014 30.06.2014
104 G 01.07.2014 31.12.2014
Is there any code which can be written to achieve this?
I tried doing this at report level using exception aggregation but its not working since we require Plan field in output and we r able to count correctly only when plan is removed from report output.
Thanks,
Tanvi
2014 Dec 17 12:08 PM
Hi Tanvi,
Use a vary simple logic to get the output.
1. Sort your internal table with EMP no and Plan.
2. Delete adj. by Employee and Plan.
and apply the below logic.
data: idx type I,
no type i.
Loop at itab into wa.
idx = sy-tabix.
clear no.
loop at itab into wa1 where employee eq wa-employee.
no = no + 1.
endloop.
if no eq 1.
delete itab in idx.
endif.
endloop.
At final you will get the internal table, which is contains only employee no, who is having more then single Plan.
Regards.
Praveer.
2014 Dec 17 11:36 AM
2014 Dec 17 11:51 AM
Some moderator deleted that thread so i lost all solutions which were there.
Thanks,
Tanvi
2014 Dec 17 12:00 PM
Its site moderator
he deleted it... as per site rule u can ask how to write code and u just copy paste it...
i just said use counter with at end of statement..
as i said keep data as u shown in sample data in one internal table say it1..
the define another table it2 with fields emp code and count
define 1 variable w_cnt type i.
along with varibale each for your it1 fields.
sort it1 by emp code.
loop at it1.
w_cnt = w_cnt + 1.
pass it1 data to to each variable
e.g w_empcode = it_empcode.
and so on
the use at end of emp code
it2-empcode = w_empcode
it2-count = w_cont.
append it2.
clear it2
clear all vairable along with w_cnt.
endat.
endloop.
then delete it2 where count = 1
again loop it1
read it2 with key emp code
if sy-subrc ne 0
delete it1.
endloop
what you will get is your desire o/p
hope this will help
Regards
Gaurav
2014 Dec 17 12:08 PM
Hi Tanvi,
Use a vary simple logic to get the output.
1. Sort your internal table with EMP no and Plan.
2. Delete adj. by Employee and Plan.
and apply the below logic.
data: idx type I,
no type i.
Loop at itab into wa.
idx = sy-tabix.
clear no.
loop at itab into wa1 where employee eq wa-employee.
no = no + 1.
endloop.
if no eq 1.
delete itab in idx.
endif.
endloop.
At final you will get the internal table, which is contains only employee no, who is having more then single Plan.
Regards.
Praveer.
2014 Dec 17 12:24 PM
HI tanvi,
kindly check the following code:
i have created three internal tables
*&---------------------------------------------------------------------*
*& Report ZR_DISTINCT_RECORDS1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZR_DISTINCT_RECORDS1.
TYPES: Begin of ty,
employee TYPE C LENGTH 10,
plan TYPE C,
start_date like sy-datum,
end_date TYPE D,
End of ty.
TYPES: Begin of ty1,
employee TYPE C LENGTH 10,
cnt type i,
End of ty1.
DATA: itab TYPE STANDARD TABLE OF ty,
itab1 TYPE STANDARD TABLE OF ty1,
itab2 TYPE STANDARD TABLE OF ty,
wa TYPE ty,
wa1 TYPE ty1.
wa-employee = '101'.
wa-plan = 'A'.
WA-start_date = '01.01.2014'. .
WA-end_date = '31.03.2014'.
APPEND wa to itab.
wa-employee = '101'.
wa-plan = 'B'.
WA-start_date = '01.01.2014'.
WA-end_date = '31.07.2014'.
APPEND wa to itab.
wa-employee = '101'.
wa-plan = 'C'.
WA-start_date = '01.08.2014'.
WA-end_date = '31.12.2014'.
APPEND wa to itab.
wa-employee = '102'.
wa-plan = 'D'.
WA-start_date = '01.01.2014'.
WA-end_date = '30.04.2014'.
APPEND wa to itab.
wa-employee = '103'.
wa-plan = 'E'.
WA-start_date = '01.01.2014'.
WA-end_date = '31.12.2014'.
APPEND wa to itab.
wa-employee = '104'.
wa-plan = 'F'.
WA-start_date = '01.01.2014'.
WA-end_date = '30.06.2014'.
APPEND wa to itab.
wa-employee = '104'.
wa-plan = 'G'.
WA-start_date = '01.07.2014'.
WA-end_date = '31.12.2014'.
APPEND wa to itab.
SORT itab by employee.
LOOP at itab into wa.
write: / wa-employee, wa-plan, wa-start_date , wa-end_date.
WA1-EMPLOYEE = wa-employee.
wa1-cnt = 1.
COLLECT WA1 INTO ITAB1.
ENDLOOP.
DELETE itab1 where cnt = 1.
LOOP at itab1 into wa1.
LOOP at itab into wa where employee = wa1-employee.
APPEND WA TO ITAB2.
ENDLOOP.
ENDLOOP.
WRITE: / 'FINAL RESULT2'.
LOOP at itab2 into wa.
write: / wa-employee, wa-plan, wa-start_date , wa-end_date.
ENDLOOP.
2014 Dec 17 12:26 PM
Hii Tanvi,
(2 internal tabls, 1variable a1 work space)
LOOP AT gt1 INTO gw1. (gt1 is actual table)
if sy-tabix = 1.
emp = gw1-emp.
CONTINUE.
endif.
if emp = gw1-emp.
APPEND gw1-emp to gt.
DELETE gt1 WHERE emp = gw1-emp.
else.
DELETE gt1 WHERE emp = emp. (internal table field emp = varable emp)
emp = gw1-emp.
ENDIF.
ENDLOOP.
SELECT * FROM ... INTO CORRESPONDING FIELDS OF TABLE gt1 .
uline.
write: 'the result is' .
loop at gt into emp.
loop at gt1 into gw1 where emp = emp.
WRITE: /,gw1 .
endloop.
endloop.
Regards
Saleem