Internal Table Group By

Hi everyone,
  I got a FM with a table parameter and i want to perform some "group by" operation(like sql group by) on it. Any suggestion??
Regards,
Kit

COLLECT
Syntax
COLLECT wa INTO itab [result].
Effect
This statement inserts the contents of a work area wa either as single row into an internal table itab or <b>adds the values of its numeric components to the corresponding values of existing rows with the same key</b>. As of Release 6.10, you can use result to set a reference to the inserted or changed row in the form of a field symbol or data reference.
<b>Prerequisite for the use of this statement is that wa is compatible with the row type of itab and all components that are not part of the table key must have a numeric data type (i, p, f).</b>
In standard tables that are only filled using COLLECT, the entry is determined by a temporarily created hash administration. The workload is independent of the number of entries in the table. The hash administration is temporary and is generally invalidated when the table is accessed for changing. If further COLLECT statements are entered after an invalidation, a linear search of all table rows is performed. The workload for this search increases in a linear fashion in relation to the number of entries.
In sorted tables, the entry is determined using a binary search. The workload has a logarithmic relationship to the number of entries in the table.
In hashed tables, the entry is determined using the hash administration of the table and is always independent of the number of table entries.
If no line is found with an identical key, a row is inserted as described below, and filled with the content of wa:
In standard tables the line is appended.
In sorted tables, the new line is inserted in the sort sequence of the internal table according to its key values, and the table index of subsequent rows is increased by 1.
In hashed tables, the new row is inserted into the internal table by the hash administration, according to its key values.
If the internal table already contains one or more rows with an identical key, those values of the components of work area wa that are not part of the key, are added to the corresponding components of the uppermost existing row (in the case of index tables, this is the row with the lowest table index).
The COLLECT statement sets sy-tabix to the table index of the inserted or existing row, in the case of standard tables and sorted tables, and to the value 0 in the case of hashed tables.
Outside of classes, you can omit wa INTO if the internal table has an identically-named header line itab. The statement then implicitly uses the header line as the work area.
COLLECT should only be used if you want to create an internal table that is genuinely unique or compressed. In this case, COLLECT can greatly benefit performance. If uniqueness or compression are not required, or the uniqueness is guaranteed for other reasons, the INSERT statement should be used instead.
The use of COLLECT for standard tables is obsolete. COLLECT should primarily be used for hashed tables, as these have a unique table key and a stable hash administration.
If a standard table is filled using COLLECT, it should not be edited using any other statement with the exception of MODIFY. If the latter is used with the addition TRANSPORTING, you must ensure that no key fields are changed. This is the only way to guarantee that the table entries are always unique and compressed, and that the COLLECT statement functions correctly and benefits performance. The function module ABL_TABLE_HASH_STATE can be used to check whether a standard table is suitable for editing using COLLECT.
Example
Compressed insertion of data from the database table sflight into the internal table seats_tab. The rows in which the key components carrid and connid are identical are compressed by adding the number of occupied seats to the numeric component seatsocc.
DATA: BEGIN OF seats,
        carrid   TYPE sflight-carrid,
        connid   TYPE sflight-connid,
        seatsocc TYPE sflight-seatsocc,
      END OF seats.
DATA seats_tab LIKE HASHED TABLE OF seats
               WITH UNIQUE KEY carrid connid.
SELECT carrid connid seatsocc
       FROM sflight
       INTO seats.
  COLLECT seats INTO seats_tab.
ENDSELECT.
Exceptions
Catchable Exceptions
CX_SY_ARITHMETIC_OVERFLOW
Cause: Overflow in integer field during totals formation
Runtime Error: COLLECT_OVERFLOW
Cause: Overflow in type p field during totals formation
Runtime Error: COLLECT_OVERFLOW_TYPE_P
Non-Catchable Exceptions
Cause: COLLECT used for non-numeric fields
Runtime Error: TABLE_COLLECT_CHAR_IN_FUNCTION

Similar Messages

  • How to pass the values from internal table to field groups

    hi all,
    how can i pass the internal  table values to field groups?
    already field groups are holding some values.. INSERT STATEMENT IS NOT WORKING as it is ovewriting the existing values..
    Use full answers will be rewared.
    Thanks.
    Moderator message - duplicate post locked
    Edited by: Rob Burbank on Jun 23, 2009 9:51 AM

    Hi,
    You can use INSERT statement to put a work area of an Internal table in Field-group
    and use Extract to get info out of it.
    Hope it helps,
    Raj

  • Grouping of two rows of internal table

    Hi all,
    I am having a requirement in which I want to group two rows of an internal table and assign a pointer to the two rows.
    This pointer variable will then be passed to ALV.
    Help reqd.
    regards.

    Hi Gaurav,
    Hope the below code helps:
    TYPES :BEGIN OF TY_ITAB2,
    DATA(400),
    END OF TY_ITAB2.
    DATA: ITAB3 TYPE TY_ITAB2 OCCURS 0 WITH HEADER LINE
    Loop at itab1.
    ITAB3-DATA = ITAB1-LABEL.
    APPEND ITAB3.
    ITAB3-DATA = ITAB1-MATNR.
    APPEND ITAB3.
    endloop.
                        or
    You can create a deep internal table. You can declare one Column as an internal table and store the NOTES in that Internal table for each row.
    http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb366d358411d1829f0000e829fbfe/content.htm
    you can check the example in the link
    Kindly Reward Points If You Find The Reply Helpful.
    Cheers,
    Chaitanya.

  • Grouping of rows of internal table.

    Hi all,
    I am having a requirement in which I want to group two rows of an internal table and assign a pointer to the two rows.
    This pointer variable will then be passed to ALV.
    Help reqd.
    regards.

    hi,
    AT - itab:
    push F1 to look at the documentation for grouping
    then assign the value to a field-symbol.

  • Urgent problem: grouping by fields of internal table

    Hi friend,
    i have a urgent problem and i hope that you can help me out:
    i have an internal table containing data, and i sorted it by 3 fields.
    now what i want to do is kind of grouping that means that all the lines that are the same should be written into another internal table like this:
    A
    A
    B
    B
    B
    C
    D
    would give me:
    A                       1
    A                       1
    B                       2
    B                       2
    B                       2
    C                       3
    D                       4
    How could i do this ( especially avoiding nested loops ? ) Is there a handy trick to do this efficiently ?
    thank you, i will give points immediately,
    Clemens

    Hello,
    The field position is important for the fields that are using in the control break statements..Also SORT internal table is required.
    http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb381a358411d1829f0000e829fbfe/content.htm
    DATA: BEGIN OF ITAB OCCURS 0,
    MATNR TYPE MATNR,
    WERKS TYPE WERKS_D,
    VALUE TYPE NETPR,
    END OF ITAB.
    ITAB-MATNR = 'ABC'.
    ITAB-WERKS = '0100'.
    ITAB-VALUE = '10.00'.
    APPEND ITAB.
    ITAB-MATNR = '1ABC'.
    ITAB-WERKS = '0100'.
    ITAB-VALUE = '10.00'.
    APPEND ITAB.
    SORT ITAB BY MATNR WERKS.
    LOOP AT ITAB.
    AT FIRST.
      WRITE : 'AT FIRST'.
    ENDAT.
    AT NEW MATNR.
      WRITE : 'AT NEW MATERIAL NUMBER'.
    ENDAT.
    AT END OF MATNR.
      SUM.
      WRITE: / ITAB-MATNR, 'MATERIAL TOTAL - ', ITAB-VALUE.
    ENDAT.
    AT END OF WERKS.
      SUM.
      WRITE: / ITAB-WERKS, 'PLANT TOTAL - ', ITAB-VALUE.
    ENDAT.
    AT LAST.
      WRITE : 'AT LAST'.
    ENDAT.
    ENDLOOP.
    Regards,
    Deepu.K

  • Difference between the Field Group  and Internal Table.

    Hi all,
    Can anybody tell me the difference between the Field group and Internal table and when they will used?
    Thanks,
    Sriram.

    Hi
    Internal Tables: They are used to store record type data in tabular form temporarily in ABAP programming. Or we can say, it stores multiple lines of records for temporary use in ABAP programming.
    A field group is a user-defined grouping of characteristics and basic key figures from the EC-EIS or EC-BP field catalog.
    Use
    The field catalog contains the fields that are used in the aspects. As the number of fields grows, the field catalog becomes very large and unclear. To simplify maintenance of the aspects, you can group fields in a field group. You can group the fields as you wish, for example, by subject area or responsibility area. A field may be included in several field groups.
    When maintaining the data structure of an aspect, you can select the field group that contains the relevant characteristics and basic key figures. This way you limit the number of fields offered.
    Regards
    Ashish

  • Group by and internal table

    Hi everyone,
    Please help me. Is it possible to select data from database table into an internal table with group by?
    For example:
    select carrid connid SUM(seatsocc)
    from sflight
    into(internal-table-carrid, internal-table-connid, sum)
    group by carrid connid
    where fldate between ***
    endselect
    thank you in advance!
    Linda

    yes,
    select carrid connid SUM( seatsocc )      
    from sflight                              
    into (sflight-carrid, sflight-connid, sflight-seatsocc)
    where fldate = sy-datum group by carrid connid
    endselect.     
    The problems with yours were:
    . no space after SUM(
    . group by in wrong spot (before where)
    Here's the into table version:
    data ts type table of sflight with header line.
    select carrid connid SUM( seatsocc ) as seatsocc
    from sflight                               
    into corresponding fields of table ts      
    where fldate = sy-datum group by carrid connid
    This is not the most efficient way though. It is probably better to select into a table and sort and aggregate into another table in abap.

  • Grouping by column in Internal table

    Dear folks,
    I have an internal table and my aim is to group its contents (just like we have the GROUP BY statement in normal sql).
    How can we group the contents in an internal table?.
    any hint will be appreciated.
    Thanks
    Praveen

    Hi Praveen A N,
        Sort your internalt table by your requirment.
    See this example program for sort.
    DATA: BEGIN OF LINE,
            LAND(3)  TYPE C,
            NAME(10) TYPE C,
            AGE      TYPE I,
            WEIGHT   TYPE P DECIMALS 2,
          END OF LINE.
    DATA ITAB LIKE STANDARD TABLE OF LINE WITH NON-UNIQUE KEY LAND.
    LINE-LAND = 'G'.   LINE-NAME   = 'Hans'.
    LINE-AGE  = 20.    LINE-WEIGHT = '80.00'.
    APPEND LINE TO ITAB.
    LINE-LAND = 'USA'. LINE-NAME   = 'Nancy'.
    LINE-AGE  = 35.    LINE-WEIGHT = '45.00'.
    APPEND LINE TO ITAB.
    LINE-LAND = 'USA'. LINE-NAME   = 'Howard'.
    LINE-AGE  = 40.    LINE-WEIGHT = '95.00'.
    APPEND LINE TO ITAB.
    LINE-LAND = 'GB'.  LINE-NAME   = 'Jenny'.
    LINE-AGE  = 18.    LINE-WEIGHT = '50.00'.
    APPEND LINE TO ITAB.
    LINE-LAND = 'F'.   LINE-NAME   = 'Michele'.
    LINE-AGE  = 30.    LINE-WEIGHT = '60.00'.
    APPEND LINE TO ITAB.
    LINE-LAND = 'G'.   LINE-NAME   = 'Karl'.
    LINE-AGE  = 60.    LINE-WEIGHT = '75.00'.
    APPEND LINE TO ITAB.
    PERFORM LOOP_AT_ITAB.
    SORT ITAB.
    PERFORM LOOP_AT_ITAB.
    SORT ITAB.
    PERFORM LOOP_AT_ITAB.
    SORT ITAB STABLE.
    PERFORM LOOP_AT_ITAB.
    SORT ITAB DESCENDING BY LAND WEIGHT ASCENDING.
    PERFORM LOOP_AT_ITAB.
    FORM LOOP_AT_ITAB.
      LOOP AT ITAB INTO LINE.
        WRITE: / LINE-LAND, LINE-NAME, LINE-AGE, LINE-WEIGHT.
      ENDLOOP.
      SKIP.
    ENDFORM.
    The output is:
    G   Hans               20         80.00
    USA Nancy              35         45.00
    USA Howard             40         95.00
    GB  Jenny              18         50.00
    F   Michele            30         60.00
    G   Karl               60         75.00
    F   Michele            30         60.00
    G   Hans               20         80.00
    G   Karl               60         75.00
    GB  Jenny              18         50.00
    USA Howard             40         95.00
    USA Nancy              35         45.00
    F   Michele            30         60.00
    G   Karl               60         75.00
    G   Hans               20         80.00
    GB  Jenny              18         50.00
    USA Howard             40         95.00
    USA Nancy              35         45.00
    F   Michele            30         60.00
    G   Karl               60         75.00
    G   Hans               20         80.00
    GB  Jenny              18         50.00
    USA Howard             40         95.00
    USA Nancy              35         45.00
    USA Nancy              35         45.00
    USA Howard             40         95.00
    GB  Jenny              18         50.00
    G   Karl               60         75.00
    G   Hans               20         80.00
    F   Michele            30         60.00
    Reward if it is useful,
    Mahi.

  • Diff Between Internal Table with Occurs 0 & Field Groups

    Hi,
    Is there really any difference between just using an internal table with an OCCURS 0 statement-- which would write the entire table to paging space-- and using field-groups? How is Field-Groups is more effective than Internal tables with occurs 0 when it comes to performance?
    Could anybody please give some information regarding above question?
    Thanks,
    Mohan.

    hi,
    occurs 0 means it wont create any extra memory. based on the records only the memory is allocated to internal tables at run time. but when an internal table is created it can hold data of type to which it is declared.
    i.e data: itab like mara occurs 0 with header line.
    can take data only from mara table
    we can also do in another way as using types keyword we can declare a standard structure and create a internal table of that type. its also not that useful as we have to change the structure depending on changes for storing data.
    for this purpose field symbols are used. field symbols can hold any data means that they can point to tables, fields, any standard or user-defined types. field symbols actually points to respective types by which we can directly access to that types using field symbols.
    filed symbols works more faster than internal tables.
    if helpful reward some points.
    with regards,
    Suresh.A

  • Differences between Internal table with Occurs 0 and Field-Groups?

    Is there really any difference between just using an internal table with an OCCURS 0 statement-- which would write the entire table to paging space-- and using field-groups? How is Field-Groups is more effective than Internal tables with occurs 0 when it comes to performance?
    Could anybody please give some information regarding above question?
    Thanks,
    Surya.

    hi,
    occurs 0 means it wont create any extra memory. based on the records only the memory is allocated to internal tables at run time. but when an internal table is created it can hold data of type to which it is declared.
    i.e data: itab like mara occurs 0 with header line.
    can take data only from mara table
    we can also do in another way as using types keyword we can declare a standard structure and create a internal table of that type. its also not that useful as we have to change the structure depending on changes for storing data.
    for this purpose field symbols are used. field symbols can hold any data means that they can point to tables, fields, any standard or user-defined types. field symbols actually points to respective types by which we can directly access to that types using field symbols.
    filed symbols works more faster than internal tables.
    if helpful reward some points.
    with regards,
    Suresh.A

  • Table name for Internal order group and Profit center group

    Hello Friends,
    Could any one provide me the table for Internal order group and Profit center group.
    We are developing new customized report and user requested internal order group and Profit center group in the selection criteria.
    I have checked for this tables but found only these fields in structures.
    Thanks in advance,
    Ravi Kiran.

    Or use FM [G_SET_TREE_IMPORT|http://www.sdn.sap.com/irj/scn/advancedsearch?query=g_set_tree_import] to read the hierarchy/Group. (Read FM and FG documentation, you can also add break-point and call some S_ALRxxx transaction which use this FM for the objects you need).
    Regards,
    Raymond

  • Grouping records in an internal table

    Dear all,
    Pls follow the code and suggest me to get the specified format.
    Consider that  i have created a view to populate the reqd data and collect those in an internal table and do a calculation for one field and modify the internal table.
    when i pass this internal table to the function reuse_alv_grid , it throws out a report. But if i need to group the records of the internal table based on three fields, can i do that , if so , how should i do that, if not what method shud i adopt to get the grouped listing in ALV.
    kindly give your views.
    Code goes here..........
    select distinct edatu j_3akvgr6 mvgr4 matnr bismt vbeln auart bstkd bstkd_e ihrez_e
    bstdk_e bstdk bezei vrvez brgew ntgew volum werks kwmeng j_3asize posnr
    INTO CORRESPONDING FIELDS OF TABLE  itabdelshd
    from ZVDELISHED
    where J_3AKVGR6 IN CUSTOMER AND AUART IN ORDTYP
    AND BISMT IN STYLE  AND MATNR in FGMtrl AND bstdk_e IN PLNDATE
    AND J_3ASIZE IN GRIDVAL and werks in plant and mvgr4 in ppmon and
    bstkd in cpono and vbeln in Sales.
    LOOP AT itabdelshd INTO wadelshd.
          select sum( wmeng ) as tqty into wasumqty-tqty
          from vbep where edatu = wadelshd-edatu and vbeln = wadelshd-vbeln
          and j_3asize = wadelshd-j_3asize.
          select sum( lfimg ) as dqty into wasumqty-dqty
          from lips where vbelv = wadelshd-vbeln and j_3asize = wadelshd-j_3asize
          wadelshd-PLND_QTY = wasumqty-tqty - wasumqty-dqty.
          wadelshd-PLND_QTY = wadelshd-wmeng - wadelshd-lfimg.
          wadelshd-COLOR = wadelshd-j_3asize(4).
          MODIFY itabdelshd  FROM  wadelshd.
    ENDLOOP.
      PERFORM build_fieldcatalog.
      PERFORM build_layout.
      PERFORM display_alv_report.

    Dear Jorge,
    It is working out, But i need to do the sum for the grouped data, for instance , if the below data is grouped data seen in the alv,
    sales ord      | color    line.Item|  Ord.qty   | Pld.Qty     | Date          "
    | 0001000551  | 002V | 100       | 6,800.000 | 1,200.000 | 25.09.2006"
    | 0001000551  | 002V | 100       | 6,800.000 | 2,500.000 | 25.09.2006"
    | 0001000551  | 002V | 100       | 6,800.000 | 3,100.000 | 25.09.2006"
    | 0001000551  | 003K | 200       | 3,200.000 | 700.000    | 25.09.2006"
    | 0001000551  | 003K | 200       | 3,200.000 | 1,200.000 | 25.09.2006"
    | 0001000551  | 003K | 200       | 3,200.000  | 1,300.000 | 25.09.2006"
    and if want to sum the the planed qty based on the line.Item Grouping, meaning, can i get the report in the below form.
    sales ord      | color    line.Item|  Ord.qty   | Pld.Qty     | Date          "
    | 0001000551  | 002V | 100       | 6,800.000 | 6,800.000 | 25.09.2006"
    | 0001000551  | 003K | 200       | 6,800.000 | 3,200.000 | 25.09.2006"
    what should i do to get this form.

  • Grouping fields of the internal table

    hi all,
    i ' m using an internal table, and y need to group fields like the following example:
    what i have:
    werks arbpl         aufnr
    ca01  ka mecha   1
    ca01  ka mecha   1
    ca01  ka li           1
    ca01  ka li           1
    what i need:
    werks  arbpl          aufnr
    ca01    ka mecha   2
    ca01    ka li           2
    can anyone help me?
    Regards,
    Roxana.

    See the below example code for Grouping all fields:
    REPORT  ZTEST_IEVENTS no standard page heading
                          line-count 40(2).
    tables : vbap.
    data : begin of i_vbap occurs 0,
           vbeln like vbap-vbeln,
           posnr like vbap-posnr,
           matnr like vbap-matnr,
           kwmeng like vbap-kwmeng,
           netpr like vbap-netpr,
           end of i_vbap.
    data wa_vbap like line of  i_vbap.
    data v_flag type c.
    select-options s_vbeln for vbap-vbeln obligatory.
    start-of-selection.
    select vbeln
           posnr
           matnr
           kwmeng
           netpr from vbap
           into table i_vbap
           where vbeln in s_vbeln.
    sort i_vbap by vbeln posnr.
    end-of-selection.
    loop at i_vbap.
    move i_vbap to wa_vbap.
    at first.
    write:/2 'Order #',15 'Item #',28 'Material #',50 'Qty', 70 'Net value'.
    skip 1.
    endat.
    at new vbeln.
    write:/2 wa_vbap-vbeln,15 wa_vbap-posnr,28 wa_vbap-matnr,
            47 wa_vbap-kwmeng,65 wa_vbap-netpr.
    v_flag = 'X'.
    endat.
    if v_flag ne 'X'.
    write:/15 wa_vbap-posnr,28 wa_vbap-matnr,
            47 wa_vbap-kwmeng,65 wa_vbap-netpr.
    endif.
    at end of vbeln.
    sum.
    skip 1.
    write:/5 'Sub totals', 47 i_vbap-kwmeng,65 i_vbap-netpr.
    skip 1.
    endat.
    at last .
    skip 1.
    sum.
    write:/5 'Grand Totals',47 i_vbap-kwmeng,65 i_vbap-netpr.
    endat.
    clear v_flag.
    endloop.

  • Group by internal table

    Hi ..
    How can  I group an internal table instead of a similar field. For example I want to move internal table columns by same belnr's .

    Hi Yusuf
    Am not sure if this is what you are looking for, but please try with offset.
    Eg:
    Considering an example whereby Int.Table IT_VBAK holds VBELN(Sales Document Numbers) & Int.Table IT_OBJNR holds the Status object numbers.
    Fields in both internal tables might not match to group but field OBJNR has sales document numbers as a part.
    In this case we can go as below:
    LOOP AT it_objnr.
         READ TABLE it_vbak WITH KEY vbeln = it_objnr-objnr(10). <--- First 10 chars of OBJNR is sales document number
    ENDLOOP.
    In the above example, field lengths are different, hence we use OFFSET operations to compare.
    Considering another example, Plant is reffered as WERKS in few tables and as BWKEY in few tables in this case we can approach this way:
    LOOP AT it_marc.
         READ TABLE it_mbew WITH KEY matnr = it_marc-matnr
                                     bwkey = it_marc-werks.
    ENDLOOP.
    Hope this is what you are looking for.
    Do correct me if my understanding is wrong.
    Kind Regards
    Eswar

  • Extract datasets?? Internal Tables??.. & Field Groups??..

    Hello Dear ABAP Ace's,
    Please let me know if what are differences between Extract datasets, Internal Tables, & Field Groups????? And what are the similarities?? Also let me know the uses of extract datasets & Field groups???
    Thanks in advance.
    Regards.
    Farooq

    Hi,
    There are two ways of processing large quantities of data in ABAP - either using internal tables or extract datasets.
    An internal table is a dynamic sequential dataset in which all records have the same structure and a key. They are part of the ABAP type concept. You can access individual records in an internal table using either the index or the key.
    Extracts are dynamic sequential datasets in which different lines can have different structures. Each ABAP program may currently only have a single extract dataset. You cannot access the individual records in an extract using key or index. Instead, you always process them using a loop.
    Check these links :
    http://help.sap.com/saphelp_nw2004s/helpdata/en/9f/db9ede35c111d1829f0000e829fbfe/frameset.htm
    http://www.geocities.com/SiliconValley/Grid/4858/sap/ABAPCode/Fieldgroups.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3ca6358411d1829f0000e829fbfe/frameset.htm
    Regards
    L Appana

Maybe you are looking for

  • Note oss 994326 - IAOM 028 Object not known in CO

    Hi, i would use note 994326 - IAOM 028 Object not known in CO to clear the field EXT_OBJECT_ID, but the changing parameter C_GL_ACCOUNT is the field (type BILL_ACC_ACGL09-GL_ACCOUNT )e not the structure (type BILL_ACC_ACGL09). Can any one help me abo

  • Oracle 8.1.7.3.0 and W2K SP4

    Hello, My system administrator want to pass the SP4 (we have W2K with SP3). Is it certified Oracle 8.1.7.3.0 on W2K with SP4 ? Thank you for your help

  • Error when installing with pacman

    yesterday i update my arch and now i cant install anything this is the error error: giblib: key "7F2D434B9741E8AC" is unknown error: error al realizar la transacción (paquete no válido o dañado) Ocurrieron errores, no se actualizaron paquetes any ide

  • Transporting eclipse to another computer?

    I got a new computer so I want to transport my eclipse and the workspace to the new computer. I put them in the exactly same directories as in the old one, but it doesn't work any more. I could still work on the projects that I checked out from the S

  • Db in  External hard disk.Will it impact the performance.

    I need to create a db in external harddisk connected to the server since we ran out of space. How much will it impact the performance