2007 Aug 09 8:01 AM
Hi,
1. How to display all the odd records from the internal table.
2. delete all the records from the internal table where the salary is less than the average salary of all the employees?
Thanks.
2007 Aug 09 8:09 AM
Hi Ram,
<b>1. How to display all the odd records from the internal table.</b>
DATA V_TABIX TYPE SY-TABIX VALUE '1'.
DO.
READ TABLE ITAB INDEX V_TABIX.
IF SY-SUBRC = 0.
WRITE:/ ITAB.
V_TABIX = V_TABIX + 2.
ELSE.
EXIT.
ENDIF.
ENDDO.
<b>2. delete all the records from the internal table where the salary is less than the average salary of all the employees?
</b>
SELECT AVG( SALARY ) FROM IT_EMP INTO V_SALARY.
LOOP AT IT_EMP.
IF IT_EMP-SALARY < V_SALARY.
WRITE:/ IT_EMP.
ENDIF.
ENDLOOP.
Thanks,
Vinay
2007 Aug 09 8:07 AM
Hi,
1) To Print the ODD records
LOOP AT ITAB.
IF ITAB-FIELD / 2 <> 0.
Write:/ ITAB-FIELD1.
ENDIF.
ENDLOOP.
2) Get the total salary and divide it by number of employes, then you will get the average salary, then
LOOP AT ITAB.
IF ITAB-SALARY < AVG_SALARY.
Delete ITAB.
ENDIF.
ENDLOOP.
Regards
Sudheer
2007 Aug 09 8:09 AM
Hi Ram,
<b>1. How to display all the odd records from the internal table.</b>
DATA V_TABIX TYPE SY-TABIX VALUE '1'.
DO.
READ TABLE ITAB INDEX V_TABIX.
IF SY-SUBRC = 0.
WRITE:/ ITAB.
V_TABIX = V_TABIX + 2.
ELSE.
EXIT.
ENDIF.
ENDDO.
<b>2. delete all the records from the internal table where the salary is less than the average salary of all the employees?
</b>
SELECT AVG( SALARY ) FROM IT_EMP INTO V_SALARY.
LOOP AT IT_EMP.
IF IT_EMP-SALARY < V_SALARY.
WRITE:/ IT_EMP.
ENDIF.
ENDLOOP.
Thanks,
Vinay
2007 Aug 09 8:11 AM
hi,
1) To Print the ODD records
v_index = 1.
LOOP AT ITAB.
Read table itab index v_index.
if sy-subrc eq 0.
Write:/ ITAB-FIELD1.
endif.
v_index = v_index + 2.
ENDLOOP.
2) Get the total salary and divide it by number of employes, then you will get the average salary, then
LOOP AT ITAB.
IF ITAB-SALARY < AVG_SALARY.
Delete ITAB.
ENDIF.
ENDLOOP.
regards,
nagaraj
2007 Aug 09 8:12 AM
Hi Rams,
1. <b>How to display all odd records</b> - What does this mean? Are you trying to say that based on some condition, you want to omit few records and display only few records. If it is so, then you can go-a-head as below,
LOOP AT ITAB1 WHERE (condition).
Write statements.
ENDLOOP.
2. <b>delete all the records from the internal table where the salary is less than the average salary of all the employees.</b>
For this you need to first calculate average salary of all employees into a variable.
once you do that, then to delete please use statement given below.
DELETE ITAB1 WHERE Salary < (calculated_variable).
<i><b>Reward points for informative answers.</b></i>
Best Regards,
Ram.
2007 Aug 09 8:13 AM
TYPES: BEGIN OF ty_emp,
empid(4) TYPE n,
ename(20) TYPE c,
dept(4) TYPE c,
salary TYPE i,
tobedeleted,
END OF ty_emp.
DATA:
fs_emp TYPE ty_emp,
it_emp TYPE TABLE OF ty_emp.
fs_emp-empid = 1010.
fs_emp-ename = 'XX'.
fs_emp-dept = 'D100'.
fs_emp-salary = 10000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 1011.
fs_emp-ename = 'YY'.
fs_emp-dept = 'D100'.
fs_emp-salary = 11000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 1001.
fs_emp-ename = 'ZZ'.
fs_emp-dept = 'D200'.
fs_emp-salary = 12000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 1008.
fs_emp-ename = 'XY'.
fs_emp-dept = 'D200'.
fs_emp-salary = 10000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 1002.
fs_emp-ename = 'XZ'.
fs_emp-dept = 'D300'.
fs_emp-salary = 8000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 1005.
fs_emp-ename = 'YX'.
fs_emp-dept = 'D300'.
fs_emp-salary = 9500.
APPEND fs_emp TO it_emp.
fs_emp-empid = 1002.
fs_emp-ename = 'YZ'.
fs_emp-dept = 'D300'.
fs_emp-salary = 9500.
APPEND fs_emp TO it_emp.
fs_emp-empid = 1007.
fs_emp-ename = 'AA'.
fs_emp-dept = 'D100'.
fs_emp-salary = 10500.
APPEND fs_emp TO it_emp.
fs_emp-empid = 1003.
fs_emp-ename = 'BB'.
fs_emp-dept = 'D200'.
fs_emp-salary = 12000.
APPEND fs_emp TO it_emp.
fs_emp-empid = 1006.
fs_emp-ename = 'CC'.
fs_emp-dept = 'D100'.
fs_emp-salary = 15000.
APPEND fs_emp TO it_emp.
DATA: total_sal TYPE i VALUE 0.
DATA: avg_sal TYPE i.
DATA: emp_count TYPE i VALUE 0.
LOOP AT it_emp INTO fs_emp.
WRITE:/ fs_emp-empid,fs_emp-ename,fs_emp-dept,fs_emp-salary.
total_sal = total_sal + fs_emp-salary.
emp_count = emp_count + 1.
ENDLOOP.
SKIP 2.
avg_sal = total_sal / emp_count.
LOOP AT it_emp INTO fs_emp WHERE salary LT avg_sal.
fs_emp-tobedeleted = 'X'.
MODIFY it_emp FROM fs_emp.
ENDLOOP.
delete it_emp where tobedeleted = 'X'.
LOOP AT it_emp INTO fs_emp.
WRITE:/ fs_emp-empid,fs_emp-ename,fs_emp-dept,fs_emp-salary.
ENDLOOP.