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

latest record from database table

Former Member
0 Likes
1,499

Hi all,

I am using select statement on a database table. The table contains records, and each one has an effective date.

effective dt record Rate

01/01/1800 R1 40

<b>01/01/2007 R1 30</b>

01/01/1800 R2 40

I want to select rates corresponding to R1 and R2. For R2, the rate is 40, but i need the rate 30 and not 40 for R1, because rate 30 has the most recent effective date. Please tell me if i can do this in the select query itself...

Please help me...

Thanks

Pranati.

10 REPLIES 10
Read only

Former Member
0 Likes
1,282

Hi Pranati,

After selecting records in intenal table you can Sort them accrding to date using descending addition. loop the table & append first record into another internal table sing control breaks.

SORT itab BY effective_dt DESCENDING.

Ashvender

Read only

0 Likes
1,282

Hi Ashwender..

Thank you for your answer... but can we do this with in the select query .. can we avoid the internal table.. The reason is i am using this rates for another internal table(which has R1, R2 etc) which has thousands of records.. so i am worried about impact on performance if we use internal table.

Thanks

Pranati.

Read only

0 Likes
1,282

Hi Pranati,

If you want to avoid inernal table then you can for for code(use of MAX) suggested by Narenderan:

Ashvender

Read only

Former Member
0 Likes
1,282

you can use the aggregate function MAX.

select max( date ) rate .. from <database table> into .. where ...

Regards,

Ravi

Read only

Former Member
0 Likes
1,282

Hi,

Check this example.

This will get the latest record created date for the customer...from the table vbak..

DATA: BEGIN OF itab OCCURS 0,

kunnr TYPE kunnr,

date TYPE sydatum,

END OF itab.

SELECT kunnr MAX( erdat )

INTO TABLE itab

FROM vbak WHERE kunnr = 'ABC' OR kunnr = 'DEF'

GROUP by kunnr.

LOOP AT itab.

WRITE: / itab-kunnr, itab-date.

ENDLOOP.

You can change it accordingly for your requirement..

Thanks,

Naren

Read only

0 Likes
1,282

Hi Naren,

This is my select query..

SELECT max( zeff_dt ) bukrs zbrdn kostl hkont INTO TABLE l_t_brdncalc

FROM zfpbrdn

FOR ALL ENTRIES IN l_t_records

WHERE bukrs = l_t_records-bukrs.

zeff_dt is the effective date field. but this is giving me the following syntax error.

<b>The addition 'for all entries' excludes all aggregrate functions with the exception of

"count(*)", as the single element of the select clause.</b>

Is this possible with for all entries...(am i getting too greedy?!!)

Thanks

Pranati..

Read only

Former Member
0 Likes
1,282

Hi,

I believe you cannot use for all entries and aggerate function together..

Instead you can build a range for the bukrs...and then use IN your select statement...

Also you need to give GROUP BY if you use MAX..

Thanks,

Naren

Message was edited by:

Narendran Muthukumaran

Read only

0 Likes
1,282

Hi Naren..

That sounds great... can u pls tell me about building range for bukrs...

Thanks

Pranati

Message was edited by:

pranati dalvi

Read only

Former Member
0 Likes
1,282

Hi,

Try this..

Ranges: r_bukrs for bkpf-bukrs.

LOOP AT l_t_records.

  • Build the range.

r_bukrs-sign = 'I'.

r_bukrs-option = 'EQ'.

r_bukrs-low = l_t_records.

append r_bukrs.

ENDLOOP.

SORT r_bukrs by low.

DELETE adjacent duplicates from r_bukrs comparing low.

SELECT max( zeff_dt ) bukrs zbrdn kostl hkont INTO TABLE l_t_brdncalc

FROM zfpbrdn

WHERE bukrs IN r_bukrs

GROUP BY zbrdn. " I am not sure of the record field..But you have to give

" GROUP BY clause..give the correct field..

Hope this helps..

Thanks,

Naren

Read only

Former Member
0 Likes
1,282

Hi,

Please make sure to reward points for helpful answers..

Thanks,

Naren