Application Development 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: 

Modify the Records

Former Member
0 Kudos
115

Hi,

I want to write the program.

1. Modify the salry of all the employees whose salary is less than the average salary of all the employees , increase their current salary by 20%.

2. Display all records where Dept no is D100 where salary is GE 10000 in sorted order of salary 9In ascending Order).

This is my program.

TYPES:

BEGIN OF TY_EMP,

EMPID(4) TYPE C,

ENAME(30) TYPE C,

DEPT(4) TYPE C,

GRADE(1) TYPE C,

SALARY TYPE I,

END OF TY_EMP.

  • TABLE TYPE

DATA:

FS_EMP TYPE TY_EMP,

IT_EMP TYPE TABLE OF TY_EMP WITH HEADER LINE.

FS_EMP-EMPID = 'E100'.

FS_EMP-ENAME = 'X'.

FS_EMP-DEPT = 'D100'.

FS_EMP-GRADE = 'A'.

FS_EMP-SALARY = 10000.

APPEND FS_EMP TO IT_EMP.

FS_EMP-EMPID = 'E200'.

FS_EMP-ENAME = 'Y'.

FS_EMP-DEPT = 'D200'.

FS_EMP-GRADE = 'A'.

FS_EMP-SALARY = 11000.

APPEND FS_EMP TO IT_EMP.

FS_EMP-EMPID = 'E101'.

FS_EMP-ENAME = 'Z'.

FS_EMP-DEPT = 'D100'.

FS_EMP-GRADE = 'A'.

FS_EMP-SALARY = 12000.

APPEND FS_EMP TO IT_EMP.

FS_EMP-EMPID = 'E103'.

FS_EMP-ENAME = 'B'.

FS_EMP-DEPT = 'D200'.

FS_EMP-GRADE = 'B'.

FS_EMP-SALARY = 8000.

APPEND FS_EMP TO IT_EMP.

FS_EMP-EMPID = 'E104'.

FS_EMP-ENAME = 'B'.

FS_EMP-DEPT = 'D200'.

FS_EMP-GRADE = 'B'.

FS_EMP-SALARY = 7000.

APPEND FS_EMP TO IT_EMP.

FS_EMP-EMPID = 'E105'.

FS_EMP-ENAME = 'C'.

FS_EMP-DEPT = 'D100'.

FS_EMP-GRADE = 'C'.

FS_EMP-SALARY = 5000.

APPEND FS_EMP TO IT_EMP.

FS_EMP-EMPID = 'E100'.

FS_EMP-ENAME = 'D'.

FS_EMP-DEPT = 'D200'.

FS_EMP-GRADE = 'B'.

FS_EMP-SALARY = 8500.

APPEND FS_EMP TO IT_EMP.

FS_EMP-EMPID = 'E200'.

FS_EMP-ENAME = 'E'.

FS_EMP-DEPT = 'D300'.

FS_EMP-GRADE = 'B'.

FS_EMP-SALARY = 9000.

APPEND FS_EMP TO IT_EMP.

FS_EMP-EMPID = 'E104'.

FS_EMP-ENAME = 'F'.

FS_EMP-DEPT = 'D300'.

FS_EMP-GRADE = 'A'.

FS_EMP-SALARY = 13000.

APPEND FS_EMP TO IT_EMP.

LOOP AT IT_EMP INTO FS_EMP.

WRITE:/ FS_EMP-EMPID,FS_EMP-ENAME,FS_EMP-DEPT,FS_EMP-GRADE,FS_EMP-SALARY.

ENDLOOP.

Thanks.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
93

Hi,

1)data:tot_sal type i,

avg_sal type i,

count type i.

LOOP AT IT_EMP INTO FS_EMP.

tot_sal = tot_sal + FS_EMP-SALARY.

count = count + 1.

ENDLOOP.

avg_sal = tot_sal / count.

LOOP AT IT_EMP INTO FS_EMP where salary < avg_sal.

FS_EMP-salary = FS_EMP-salary + ( FS_EMP-salary * 20 / 100 ).

modify table it_emp from fs_emp.

ENDLOOP.

skip.

LOOP AT IT_EMP INTO FS_EMP.

WRITE:/ FS_EMP-EMPID,FS_EMP-ENAME,FS_EMP-DEPT,FS_EMP-GRADE,FS_EMP-SALARY.

ENDLOOP.

2)sort it_emp by SALARY.

loop at it_emp into fs_emp where dept = 'D100' and salary >= 10000.

WRITE:/ FS_EMP-EMPID,FS_EMP-ENAME,FS_EMP-DEPT,FS_EMP-GRADE,FS_EMP-SALARY.

ENDLOOP.

<b>reward if helpful</b>

rgds,

bharat.

4 REPLIES 4

Former Member
0 Kudos
93

Hi

In your coding u had just declared the variables but where u had compared for employee details and for their salary????

1) for 1st query use a check or comparator operator for that salary details and after that for increasing 20%



Int_salary is for calculating 20%
int_salary1 is same as salary
int_origsalary is original salray which u hav calculated already 
int_salary = 20/100.
int_salary1 = int_salary + int_origsalary

<b>Check this sample one</b>

LOOP AT int_outtab.
*    ON CHANGE OF int_outtab-ebeln OR int_outtab-vakey.
    ON CHANGE OF int_outtab-ebelp.
      SELECT knumh kschl vakey FROM konh INTO int_konh
*        FOR ALL ENTRIES IN int_outtab
        WHERE vakey = int_outtab-vakey AND
              datab <= int_outtab-bedat AND
              datbi > int_outtab-bedat AND
             ( kschl = 'JMOP' OR kschl = 'JEC1' ).
        SELECT knumh kschl kbetr FROM konp INTO int_konp
        WHERE  knumh = int_konh-knumh .
          IF  int_konp-kschl = 'JMOP'.
            ws_kbetr = int_konp-kbetr / 100 * int_outtab-netwr.
            ws_kbetr1 = ws_kbetr.
          ENDIF.
          IF int_konp-kschl = 'JEC1'.
            ws_kbetr2 = int_konp-kbetr / 100 * ws_kbetr1.
          ENDIF.
          ws_kbetr = ws_kbetr + ws_kbetr2.
        ENDSELECT.
      ENDSELECT.
    ENDON.

2) You can use Sort condition here for sorting a particular field in inetrnal table

Reward all helpfull answers

REgards

Pavan

Former Member
0 Kudos
93

Hi Rams,

I have given solution already in the previous thread that you posted with the same question many days ago. Please check that and close this thread.

<b>

Reward points if that helps,</b>

Kiran

Former Member
0 Kudos
94

Hi,

1)data:tot_sal type i,

avg_sal type i,

count type i.

LOOP AT IT_EMP INTO FS_EMP.

tot_sal = tot_sal + FS_EMP-SALARY.

count = count + 1.

ENDLOOP.

avg_sal = tot_sal / count.

LOOP AT IT_EMP INTO FS_EMP where salary < avg_sal.

FS_EMP-salary = FS_EMP-salary + ( FS_EMP-salary * 20 / 100 ).

modify table it_emp from fs_emp.

ENDLOOP.

skip.

LOOP AT IT_EMP INTO FS_EMP.

WRITE:/ FS_EMP-EMPID,FS_EMP-ENAME,FS_EMP-DEPT,FS_EMP-GRADE,FS_EMP-SALARY.

ENDLOOP.

2)sort it_emp by SALARY.

loop at it_emp into fs_emp where dept = 'D100' and salary >= 10000.

WRITE:/ FS_EMP-EMPID,FS_EMP-ENAME,FS_EMP-DEPT,FS_EMP-GRADE,FS_EMP-SALARY.

ENDLOOP.

<b>reward if helpful</b>

rgds,

bharat.

gopi_narendra
Active Contributor
0 Kudos
93

See the changes in RED color


TYPES: BEGIN OF ty_emp,
         empid(4) TYPE c,
         ename(30) TYPE c,
         dept(4) TYPE c,
         grade(1) TYPE c,
         salary TYPE i,
       END OF ty_emp.

* TABLE TYPE
DATA: it_emp TYPE TABLE OF ty_emp WITH HEADER LINE,
      fs_emp TYPE ty_emp.
 " declarations
DATA : n TYPE i. 
DATA: salary TYPE i,
net_salary TYPE i.
 " declarations

fs_emp-empid = 'E100'.
fs_emp-ename = 'X'.
fs_emp-dept = 'D100'.
fs_emp-grade = 'A'.
fs_emp-salary = 10000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 'E200'.
fs_emp-ename = 'Y'.
fs_emp-dept = 'D200'.
fs_emp-grade = 'A'.
fs_emp-salary = 11000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 'E101'.
fs_emp-ename = 'Z'.
fs_emp-dept = 'D100'.
fs_emp-grade = 'A'.
fs_emp-salary = 12000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 'E103'.
fs_emp-ename = 'B'.
fs_emp-dept = 'D200'.
fs_emp-grade = 'B'.
fs_emp-salary = 8000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 'E104'.
fs_emp-ename = 'B'.
fs_emp-dept = 'D200'.
fs_emp-grade = 'B'.
fs_emp-salary = 7000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 'E105'.
fs_emp-ename = 'C'.
fs_emp-dept = 'D100'.
fs_emp-grade = 'C'.
fs_emp-salary = 5000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 'E100'.
fs_emp-ename = 'D'.
fs_emp-dept = 'D200'.
fs_emp-grade = 'B'.
fs_emp-salary = 8500.
APPEND fs_emp TO it_emp.
fs_emp-empid = 'E200'.
fs_emp-ename = 'E'.
fs_emp-dept = 'D300'.
fs_emp-grade = 'B'.
fs_emp-salary = 9000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 'E104'.
fs_emp-ename = 'F'.
fs_emp-dept = 'D300'.
fs_emp-grade = 'A'.
fs_emp-salary = 13000.
APPEND fs_emp TO it_emp.

 " Calculations
DESCRIBE TABLE it_emp LINES n.

 " Calculations
LOOP AT it_emp INTO fs_emp.
  salary = salary + fs_emp-salary.
  CLEAR : fs_emp.
ENDLOOP.

 " Calculations
net_salary = salary / n.

 " Calculations
LOOP AT it_emp INTO fs_emp.
  IF fs_emp-salary LT net_salary.
    fs_emp-salary = fs_emp-salary + ( fs_emp-salary / 5 ).
    MODIFY it_emp FROM fs_emp TRANSPORTING salary.
  ENDIF.
  CLEAR : fs_emp.
ENDLOOP.

LOOP AT it_emp INTO fs_emp.
WRITE:/ fs_emp-empid,
        fs_emp-ename,
        fs_emp-dept,
        fs_emp-grade,
        fs_emp-salary.
ENDLOOP.

For your second requirement do as below


sort it_emp by dept.
LOOP AT it_emp INTO fs_emp where dept = 'D100' and salary GT 10000.
WRITE:/ fs_emp-empid,
        fs_emp-ename,
        fs_emp-dept,
        fs_emp-grade,
        fs_emp-salary.
ENDLOOP.

Regards

Gopi