Application Development and Automation 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: 
Read only

Need help with ABAP general coding

Former Member
0 Likes
1,775

I am not sure what the best approach would be so i am hoping you can help me out. I have a pretty simple scenario but I am not sure the best way to collect the data.

I have a table that contains material data except prices. I need to go to another table and see if their is a material price for the current period. If so I just need to modify the package. If not I need to keep looping back through the table for prior months until I find a price. i will never go back more than 1 year. My link will be

what is the best design? I was thinking of getting 2 internal tables. 1 with current prices for all the materials in my table and one with prior prices

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,741

Hi,

I think you can use the Tables MBEWH and MARDH for this purpose to find the prices by looping into internal table till you get some price.

These tables stores the previous values for that material.

use them

reward if useful

regards,

ANJI

19 REPLIES 19
Read only

Former Member
0 Likes
1,742

Hi,

I think you can use the Tables MBEWH and MARDH for this purpose to find the prices by looping into internal table till you get some price.

These tables stores the previous values for that material.

use them

reward if useful

regards,

ANJI

Read only

former_member491305
Active Contributor
0 Likes
1,741

Hi,

Try like this...

Val_date = sy-datum - 365.

Use Select MAX(DATE) price from table2 into table itab2 for all entries in itab1

where matnr = itab1-matnr

and date GE Val_DATE.

Read only

0 Likes
1,741

Then how do I get the loop to work and grap the latest day with the orice?

Read only

0 Likes
1,741

Hi,

Max(date) itself will fetch the latest day ,latest month, latest year.

If am wrong please clarify.

Read only

0 Likes
1,741

My date field is int he format mmm/yyyy. I don't have the day. will this work?

Read only

0 Likes
1,741

No it won't work.Try like this.

Select MAX(DATE) matnr from table2 into table itab2 for all entries in itab1

where matnr = itab1-matnr

and date GE Val_DATE.

select max(day) matnr price from table2 into table itab3 for all entries in itab2

where matnr = itab2-matnr and date = itab2-date.

now itab3 will contain price for the latest period for each material..

Read only

0 Likes
1,741

Here is waht i tried with MAX and this is the error

Unknown column name "MAX(FISCPER)". not determined until runtime

select MAX(FISCPER) MATERIAL PLANT FISCPER PRICE_STD PRICE_BASE INTO TABLE

T_MATPRICE_DATA

FROM /BIC/AZCO_O0100

FOR ALL ENTRIES IN DATA_PACKAGE

WHERE

MATERIAL = DATA_PACKAGE-MATERIAL AND

PLANT = DATA_PACKAGE-PLANT and

  • FISCPER = DATA_PACKAGE-FISCPER.

  • FISCYEAR = DATA_PACKAGE-FISCYEAR.

FISCPER GE VAL_DATE.

Read only

0 Likes
1,741

But you are uisng GE so it should grab the max date correct?

Read only

0 Likes
1,741

Now I get this error below.

The addition "FOR ALL ENTRIES" excludes all aggregate functions with the exception of "COUNT( * )", as the single element of the SELECT clause

here is my code

Select MATERIAL PLANT MAX( FISCPER ) PRICE_STD PRICE_BASE INTO TABLE

T_MATPRICE_DATA

FROM /BIC/AZCO_O0100

FOR ALL ENTRIES IN DATA_PACKAGE

WHERE

MATERIAL = DATA_PACKAGE-MATERIAL AND

PLANT = DATA_PACKAGE-PLANT and

FISCPER GE VAL_DATE.

Read only

0 Likes
1,741

Hi Mick,

Sorry for the mistaken Code syntax .'for all entries' won't work while using Aggregate fn.Check if the following code work for u.

SELECT material plant fiscper day INTO TABLE

t_matprice_data

FROM /bic/azco_o0100

FOR ALL ENTRIES IN data_package

WHERE

material = data_package-material AND

plant = data_package-plant AND

fiscper GE val_date.

SORT t_matprice_data BY material plant fiscper DESCENDING day DESCENDING.

DELETE ADJACENT DUPLICATES FROM t_matprice_data COMPARING material plant.

Now t_matprice_data will contain latest price for the material.

Read only

0 Likes
1,741

I will try it out. what does the 2 descending statements do/ Do you need both or just the one

Read only

0 Likes
1,741

I assume that <b>t_matprice_data</b> table contains "Period" field(MM/YYYY)

and DAY field. DESCENDING is needed for both PERIOD and DAY. To sort the specific field according to our need we can use like this.These descending stmnt. will sort both PERIOD field and DAY field in Descending order to move latest Date into Top.This will be helpful while using DELETE ADJACENT DUPLICATES stmnt.

Read only

0 Likes
1,741

No it just contains Period" field(MM/YYYY) so would I change that to be below or do i take out the 2nd

SORT T_CMATPRICE_DATA BY material plant fiscper PRICE_STD PRICE_BASE

DESCENDING FISCPER DESCENDING.

DELETE ADJACENT DUPLICATES FROM T_CMATPRICE_DATA COMPARING material

plant.

Read only

0 Likes
1,741

I wonder how do u fetch latest day for the corresponding latest period.

Is ther no 'day' field in Table.If u dont want latest 'day' for the latest 'period'

then do like below.And no need to give PRICE_STD PRICE_BASE while sorting the ITAB .

jus do like this.

SORT T_CMATPRICE_DATA BY material plant fiscper DESCENDING .

DELETE ADJACENT DUPLICATES FROM T_CMATPRICE_DATA COMPARING material plant.

Message was edited by:

Vigneswaran S

Read only

0 Likes
1,741

this will work but i need to a FM that will calculate 12 months back

Read only

Former Member
0 Likes
1,741

MIck ,

u cannt this into Coding , better to get going on Avg.Price of Material ?

u can also check MARDMBEW for current , MBEWHMARDH for History.

y i am telling this one is , Some times these table will not Updateded, due to SAP system settings , so better to go for moving avg.price of MARD.

Regards

prabhu

Read only

0 Likes
1,741

No the design they want is the way I described. they do not want avg. price. We already loded the prices inot an ODS in BW so i will be reading from this table.

why can't i do this in coding? I've done this type of logic before so why is it an issue?

Read only

dev_parbutteea
Active Contributor
0 Likes
1,741

Hi Mick,

Using two internal tables is a good approach:

- 1st table for current prices & 2nd for prior prices

- sort the two tables by period field descending

- calculate the validity_period

one example is : val_period = sy-datum - 365!

- set ld_index = 0.

- Loop at first table where period > val_period into wa_area

Loop at second table from ld_index

if period < wa_area-period.

continue.

elseif period > wa_area-period.

exit.

else period = wa_area-period.

process modifications

endif.

Reward if helpful.

Regards ,

Sooness

endloop.

endloop.

Read only

dev_parbutteea
Active Contributor
0 Likes
1,741

Ps: you may exit from both loops by setting a flag in 2nd loop then putting a check for it after 1st endloop