Skip to Content
0
Former Member
Mar 12, 2013 at 08:45 AM

use of group by and sum in sap

27 Views

Hi all ,

I m writing a program in which i have to fetch material from table mbewh with its total stock at any yrs period = 12 .code is as follow

tables : mbewh ,
mbew .


data : begin of itab occurs 100 ,
matnr like mbewh-matnr ,
bwkey like mbewh-bwkey ,
bwtar like mbewh-bwtar ,
lfgja like mbewh-lfgja ,
lfmon like mbewh-lfmon ,
lbkum LIKE mbewh-lbkum ,
total like mbewh-lbkum ,
end of itab .

data : begin of itab1 occurs 100 ,
matnr like mbewh-matnr ,
lbkum like mbewh-lbkum ,
end of itab1 .

DATA : RETURNCODE LIKE SY-SUBRC ,
field(40) type c .

data : BEGIN OF IHEADER OCCURS 10 ,
field_name(25) TYPE C,
END OF IHEADER.

refresh iheader .
clear iheader .

IHEADER-field_name = 'Material number'.
append iheader.
IHEADER-field_name = 'Plant'.
append iheader.
IHEADER-field_name = 'valuation '.
append iheader.
IHEADER-field_name = 'Year '.
append iheader.
"IHEADER-field_name = 'plant'.
"append iheader.
IHEADER-field_name = 'Month'.
append iheader.
IHEADER-field_name = 'Stock'.
append iheader.
IHEADER-field_name = 'Total_stock'.
append iheader.


select-options : matnr for mbewh-matnr ,
bwkey for mbewh-bwkey ,
bwtar for mbewh-bwtar .

parameters : lfgja like mbewh-lfgja OBLIGATORY.

refresh itab .
clear itab .
refresh itab .
clear itab .

select matnr bwkey bwtar lfgja lfmon lbkum sum( lbkum )
INTO (itab-matnr , itab-bwkey , itab-bwtar , itab-lfgja , itab-lfmon , itab-lbkum , itab-total )
FROM mbewh
WHERE matnr in matnr AND
bwkey in bwkey AND
lfgja = lfgja and
lfmon = '12'
group by (matnr)
.
append itab .
endselect .

sort itab .


CALL FUNCTION 'HR_DISPLAY_BASIC_LIST'
EXPORTING
BASIC_LIST_TITLE = SY-TITLE "JRFPH0K009969
FILE_NAME = SPACE
* HEAD_LINE1 = SY-TITLE "JRFPH0K009969
* HEAD_LINE1 = 'Employee Promotion Report'
" HEAD_LINE2 = page_head
* CURRENT_REPORT = SY-REPID

IMPORTING

RETURN_CODE = RETURNCODE

TABLES
DATA_TAB = itab
FIELDNAME_TAB = iheader
* SELECT_TAB = LIST_TAB_OUT

EXCEPTIONS

DOWNLOAD_PROBLEM = 1
OTHERS = 2 .

IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
write : ' No Data Is Present Or You Havenot Entered The Period ' .
ENDIF.

It is not giving me any compile time error when i m activating prog but when i try to run it gave me runtime errror

error : dbif_rsql_sql_error

exception : cx_sy_open_sql_db .

line which prompt error is :
select matnr bwkey bwtar lfgja lfmon lbkum sum( lbkum )

i need to find the total stock of all material (ie sum of stock of material in all plant )

please help me with it

.

Thanks in advance ,

shikha