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: 

AT NEW Statement is not working.

Former Member
0 Kudos

Hi everyone,

    Here i am trying to display the count of number entries are there against an employee number.

For ex. an employee with emp no 1000 is present in the table thrice then the count shoul be 3.

For this i used the AT NEW statement in loop.The code is below one.

Data : Count2 type i.

select * from zsr_opl into table ot_opl.

loop at it_opl.


at new emp-no.

     WRITE : / IT_OPL-EMP_NO.

ENDAT.

COUNT2 = COUNT2 + 1.

AT END.

WRITE : / COUNT2.

COUNT2 = 0.

ENDAT.

ENDLOOP.

HERE ZSR_OPL IS THE DB TABLE WHERE EMP-NO IS NOT KEY FIELD.

Moderator Message: Please be more specific about the problem - thread locked.

Message was edited by: Suhas Saha

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Add this before your loop.

Sort It_opl by EMP_no.

12 REPLIES 12

Former Member
0 Kudos

Add this before your loop.

Sort It_opl by EMP_no.

0 Kudos

The above suggestion is considering the fact that EMP_NO is the first field of the table.

0 Kudos

I done the sorting operation by emp no also. still the problem is there.

0 Kudos

Emp no is neighter the first field of the table nor key field.

0 Kudos

What is the internal; table structure?

0 Kudos

Internal  Table Structure is

Field                     key      init     data type          length

    

MANDT               X          X          CLNT               3

OP_NAME          X          X          CHAR               50

OP_DATE                                   DATS               8

EMP_NO                                   CHAR                10

EMP_NAME                              CHAR                30.

Considering the above sturcure  AT new EMP_NO will be triggered if any of the fields before or including EMP_NO Changes. i.e. It will ge triggered when any of MANDT OP_NAME OP_DATE OR EMP_NO changes.

To recifiy it. Wo have two options.

1) do the logic your self. Create a locl variable to check store prev. emp no and check to see if the current emp no is same as the previeous.

2) Create local internal table where emp no is the first field. and the rest of the fields after that. Transfer your enteries to this internal table, sort this table and then do the AT NEW. inside the loop of the this table.

0 Kudos

sort  IT_OPL by EMP_NO.

data lv_count type i.

loop at IT_OPL into WA_OPL.

AT first.

lv_empid = wa_opl-emp_no.

endat.

if lv_empid = wa_opl-emp_no.

lv_count = lv_count + 1.

else.

write: 'Employee Id' lv_empid 'Count:' lv_count.

lv_count = 1.

lv_empid = wa_opl-emp_no.

endif.

endloop.

Former Member
0 Kudos

Hi Try this.

sort  IT_OPL by EMP_NO.

loop at IT_OPL into WA_OPL.

at new emp-no.

     WRITE : / IT_OPL-EMP_NO.

     COUNT2 = COUNT2 + 1.

ENDAT.

AT END OF EMP-NO..

WRITE : / COUNT2.

ENDAT.

ENDLOOP.

0 Kudos

HI Hasan,

     thanks for ur reply,

i think you did'nt get point.

I want to display the count of an employee that exists in the table against the emp No.

0 Kudos

Hi,

try this code...

SORT  IT_OPL BY EMP_NO.

LOOP AT IT_OPL INTO WA_OPL.

AT NEW EMP_NO.

                                WRITE : / IT_OPL-EMP_NO.

                ENDAT.

COUNT2 = COUNT2 + 1.

               AT END OF EMP-NO..

                                 WRITE : / COUNT2.

                                  COUNT2 = 0.

               ENDAT.

  ENDLOOP.

Former Member
0 Kudos

Hi,

So, is this what you want?

EMP_NO          COUNT

1000                      1

1000                      2

1000                      3

Please correct if I'm wrong.

Jake.