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: 

Sort

former_member125931
Active Participant
0 Kudos
353

Hi All,

I am doing sum using collect statement,

here I am having two fields one is text and another one is numeriacal

I done like below

text field numerial field

a 1

b 2

c 2

a 2

a 8

a 3

so here I got a = 14,b = 2,c = 2.

I done sort by using text field

but what my problem is I need show the values in desending order of numerial field,

for that I done again sort by taking all these values into another internal table,

but again the data chaged as it is how before adding these field,

pls any one help me.

Thanks&regds,

Nicky.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
246

Instead of using collect - go for control break events.

Table contains two fields text & value.

sort itab by text.

loop at itab into wa.
  wa1 = wa.
  sum = sum + wa-value.
  at end of text.
    wa1-text = wa-text.
    wa1-value = sum.
    append wa1 to itab1.
    clear sum.
  endat.
  clear: wa, wa1.
endloop.

sort itab1 by value descending.

reward if useful.

28 REPLIES 28

Former Member
0 Kudos
246

Hello,

Though, it is very difficult to understand what exactly U are looking for..

I'm assuming that there is some problem and for that try using the below logic:

SORT by text field numeric field DESCENDING.

Let us know, if this helps.

<removed_by_moderator>

Rgds,

Raghu.

Edited by: Julius Bussche on Jun 24, 2008 7:04 PM

0 Kudos
246

Hi,

I used as you suggested but no nuse.

Thanks&regds,

Nicky.

former_member181995
Active Contributor
0 Kudos
246

Snicky,

No need of another itab.

after doing SUM you can sort by decending at numeric field.

Amit.

0 Kudos
246

Hi

Can you expalin me how it is pls.

Thanks&regds,

Nicky.

Former Member
0 Kudos
246

You can first sort by text field and then by numerical field DESCENDING.

<removed_by_moderator>

Edited by: Julius Bussche on Jun 24, 2008 7:10 PM

0 Kudos
246

Hi,

I done that,it became same as earlier before done sum,it means it sorted as desending order but no sum on similar fields.

Thanks&regds,

Nicky.

Former Member
0 Kudos
246

Hi,

After your Collect Statement write the following statement.

sort <second_internal_table> by text field.

<removed_by_moderator>

Warm Regards

R Adarsh

Edited by: Julius Bussche on Jun 24, 2008 7:11 PM

0 Kudos
246

Hi Adarsh,

Can you expalin me bit,I tried as u suggested but no use.

Thanks&regds,

Nicky.

Former Member
0 Kudos
246

hiiii

just use AT NEW fieldname.

& SUM..it will give you correct result.

like below

loop at..

at new field1.

sum.

write

endloop.

<removed_by_moderator>

thx

twinkal

Edited by: Julius Bussche on Jun 24, 2008 7:11 PM

Former Member
0 Kudos
246

Hi,

You can do the desired output like below way-

First sort the internal table by numerical field(Field 2) Descending in Descending Order.

After that sort Internal table again By Text Field (Field 1)

in Ascending order.

<removed_by_moderator>

Regards,

Sujit

Edited by: Julius Bussche on Jun 24, 2008 7:09 PM

JozsefSzikszai
Active Contributor
0 Kudos
246

hi,

this is what you need at the end?

a 14

b 2

c 2

(numbers in descending order)

in this case you have to do:

SORT itab BY text.
COLLECT itab.
SORT itab BY number descending.

hope this helps

ec

0 Kudos
246

Hi,

Here I am using like below

Sort itab by text.

loop at itab into wa.

collect wa into itab.

sort itab by num.

endloop.

but here numerical value became increment.

can u suggest me how to proceed.

Thanks&regds,

Nicky.

0 Kudos
246

-if the internal table has header line, than forget the LOOP and just do, what I suggested above

-if the internal table has NO header line, than you'll need a 2nd internal table (same as the first) and you have to code like:

Sort itab by text.

loop at itab into wa.

collect wa into itab2.

endloop.

sort itab2 by num DESCENDING.

pls. see the differences to your code:

COLLECT into itab2

SORT outside the LOOP and DESCENDING

0 Kudos
246

Hi Eric,

It is not doing any sum.

Thanks&regds,

Nicky.

0 Kudos
246

how did you define the internal table? probably the number field is not defined as type n/p/i ?

0 Kudos
246

HI Eric,

collect statement is not working.

thanks&regds,

Nicky.

0 Kudos
246

just copy here the internal table definition.

0 Kudos
246

Hi Eric,

Numeric field is decimal type.

0 Kudos
246

I ask you third time: Copy here the definition of the internal table! The whole table, not just the number field!

0 Kudos
246

HI Eric,

I am getting these values from functionmodule by providing input values,

This is the structure of that FM

KURZTEXT1 CHAR40 CHAR

QMTXT QMTXT CHAR

AUSZT2 ZDEC DEC

QMNUM QMNUM CHAR

KURZTEXT2 CHAR40 CHAR

KURZTEXT3 CHAR40 CHAR

KURZTEXT4 CHAR40 CHAR

ZSUM ZDEC DEc 13 2

PER ZDEC DEC

SUM1 ZDEC DEC 13 2

______________________________ ______________________________ ____________________________________________________________________________________________________________________________________

0 Kudos
246

HI,

Here KURZTEXT1 is text field,

AUSZT2 is numerical field.

0 Kudos
246

As per your original request/specs


DATA:
  BEGIN OF itab_rec,
    text      TYPE c,
    num       TYPE i,
  END OF itab_rec.
DATA:
  itab   LIKE STANDARD TABLE OF itab_rec,
  sumtab LIKE STANDARD TABLE OF itab_rec.

START-OF-SELECTION.

  PERFORM build_itab.

  REFRESH sumtab.

  LOOP AT itab INTO itab_rec.
    COLLECT itab_rec INTO sumtab.
  ENDLOOP.

  SORT sumtab DESCENDING BY num.

  LOOP AT sumtab INTO itab_rec.
    WRITE:/ itab_rec-text, itab_rec-num.
  ENDLOOP.

*&---------------------------------------------------------------------*
*&      Form  build_itab
*&---------------------------------------------------------------------*
FORM build_itab.
  itab_rec-text = 'a'. itab_rec-num = 1. APPEND itab_rec TO itab.
  itab_rec-text = 'b'. itab_rec-num = 2. APPEND itab_rec TO itab.
  itab_rec-text = 'c'. itab_rec-num = 2. APPEND itab_rec TO itab.
  itab_rec-text = 'a'. itab_rec-num = 2. APPEND itab_rec TO itab.
  itab_rec-text = 'a'. itab_rec-num = 8. APPEND itab_rec TO itab.
  itab_rec-text = 'a'. itab_rec-num = 3. APPEND itab_rec TO itab.

and the output


a         14                
b          2                
c          2                

0 Kudos
246

ok, just follow what Paul suggested, create an internal table with fields KURZTEXT1 and AUSZT2 only. The COLLECT does not work on your original table, because you have more fields in the table (which have different data; I assume) and these fields are also used for the comparision.

0 Kudos
246

Hi Paul,

In my case I am working in BSP,so I am explicitly defining work area and body,pls explain me how to concatenate by using body and work area,I am expecting solution from you.

Thanks&regds,

Sree.

Former Member
0 Kudos
246

hi ,

do like this,

concider ur internal table itab.then declare another internal table of same type.

now :

loop at itab.

itab1-text = itab - text.

itab1-num = itab-num.

collect itab1. -


instead of append u use collect.

clear itab1.

endloop.

sort itab1 by text .

regards,

diana.

Former Member
0 Kudos
247

Instead of using collect - go for control break events.

Table contains two fields text & value.

sort itab by text.

loop at itab into wa.
  wa1 = wa.
  sum = sum + wa-value.
  at end of text.
    wa1-text = wa-text.
    wa1-value = sum.
    append wa1 to itab1.
    clear sum.
  endat.
  clear: wa, wa1.
endloop.

sort itab1 by value descending.

reward if useful.

Former Member
0 Kudos
246

Hi SnickyUcan ,

did u try my code. it will definitely work fine..

i have done before..

try this. and let me know if any further clarification u need..

regards,

diana.

0 Kudos
246

Hi All,

Thankyou very much for all of your support.