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

Select staement with max

Former Member
0 Likes
1,464

Hi all ,

This is my select statement i want to select max(udate and utime) .How can i write this statement .

SELECT objectid username max( udate) FROM cdhdr INTO TABLE t_cdhdr FOR ALL ENTRIES IN t_cdpos WHERE objectclas ='MATERIAL' AND

objectid = t_cdpos-objectid AND changenr = t_cdpos-changenr.

Can any one guide me to resolve this issue?

9 REPLIES 9
Read only

Former Member
1,272

Hi,

You can not use 'FOR ALL ENTRIES' for aggregation and only work for count ( * ).

Please try this.


SELECT objectid username udate 
FROM cdhdr 
INTO TABLE t_cdhdr 
FOR ALL ENTRIES IN t_cdpos 
WHERE objectclas ='MATERIAL' 
  AND objectid = t_cdpos-objectid 
  AND changenr = t_cdpos-changenr.

SORT t_cdhdr by udate descending.

READ table t_cdhdr index 1.

...

OR

SELECT objectid username udate 
FROM cdhdr 
INTO TABLE t_cdhdr 
FOR ALL ENTRIES IN t_cdpos 
WHERE objectclas ='MATERIAL' 
  AND objectid = t_cdpos-objectid 
  AND changenr = t_cdpos-changenr
  AND udate in   
( SELECT MAX( udate )
  FROM cdhdr
  WHERE objectclas ='MATERIAL'
    AND objectid = t_cdpos-objectid 
    AND changenr = t_cdpos-changenr ).

Regards,

Ferry Lianto

Read only

0 Likes
1,272

Hi Ferry ,

In my select statement i am using FOR ALL ENTRIES. Is For all entries works for aggrate functions?

I need to select max ( udate and utime ) . How can i select max of these two fields.?

null

Read only

0 Likes
1,272

You can try by selecting all the records, Then sort the table and then delete the rest else the MAX.

Shreekant

Read only

Former Member
0 Likes
1,272

Hi Priya,

SELECT objectid username max ( udate ) max ( utime ) FROM cdhdr INTO TABLE t_cdhdr FOR ALL ENTRIES IN t_cdpos WHERE objectclas ='MATERIAL' AND objectid = t_cdpos-objectid AND changenr = t_cdpos-changenr.

or

SELECT objectid username udate utime FROM cdhdr INTO TABLE t_cdhdr FOR ALL ENTRIES IN t_cdpos WHERE objectclas ='MATERIAL' AND objectid = t_cdpos-objectid AND changenr = t_cdpos-changenr.

Sort itab descending by udate utime.

delete itab index NE 1.

Regards

SAB

Read only

Former Member
0 Likes
1,272

It should work just as you have it there. Just make sure there is a space on either side of udate within the parentheses. To get max of utime, just sort your table by utime descending and take the first record.

- April King

Message was edited by:

April King

Read only

Former Member
0 Likes
1,272

You have to use GROUP BY objectid username in order to achieve this as shown below.


SELECT objectid username MAX( udate ) MAX( utime )
  FROM cdhdr INTO TABLE t_cdhdr
 WHERE objectclas ='MATERIAL' GROUP BY objectid username.

But the problem here is that once you use the username in the GROUP BY clause, you will get the MAX values for that objectid and that user name not across the board.

Read only

Former Member
0 Likes
1,272

Hi,

I would say get the information for namelastnamemax(date) into internal table itab and use the below after that.

SELECT * into table itab1

from ztable

for all enteries in itab

where name = itab-name

and lastname = itab-lastname

and date = itab-date.

Regards,

Bhaskar

Read only

Former Member
0 Likes
1,272

Use code similar to the following.

  DATA t_cdpos_tmp LIKE TABLE OF t_cdpos.

  IF NOT t_cdpos[] IS INITIAL.
    t_cdpos_tmp[] = t_cdpos[].
    SORT t_cdpos_tmp BY objectid
                        changenr.
    DELETE ADJACENT DUPLICATES FROM t_cdpos_tmp
      COMPARING  objectid
                 changenr.
    SELECT objectid
           changenr
           username
           udate
      FROM cdhdr
      INTO TABLE t_cdhdr
      FOR ALL ENTRIES IN t_cdpos_tmp
      WHERE objectclas EQ 'MATERIAL'
      AND   objectid   EQ t_cdpos_tmp-objectid
      AND   changenr   EQ t_cdpos_tmp-changenr.
    IF sy-subrc EQ 0.
      SORT t_cdhdr BY objectid ASCENDING
                      changenr ASCENDING
                      udate    DESCENDING.
      DELETE ADJACENT DUPLICATES FROM t_cdhdr COMPARING objectid
                                                        changenr.
    ENDIF.
  ENDIF.

Hope this helps.

Read only

0 Likes
1,272

Please ignore my previous post because you do not need to include field CHANGENR. Use the following code instead.

  DATA t_cdpos_tmp LIKE TABLE OF t_cdpos.

  IF NOT t_cdpos[] IS INITIAL.
    t_cdpos_tmp[] = t_cdpos[].
    SORT t_cdpos_tmp BY objectid
                        changenr.
    DELETE ADJACENT DUPLICATES FROM t_cdpos_tmp
      COMPARING  objectid
                 changenr.
    SELECT objectid
           username
           udate
      FROM cdhdr
      INTO TABLE t_cdhdr
      FOR ALL ENTRIES IN t_cdpos_tmp
      WHERE objectclas EQ 'MATERIAL'
      AND   objectid   EQ t_cdpos_tmp-objectid
      AND   changenr   EQ t_cdpos_tmp-changenr.
    IF sy-subrc EQ 0.
      SORT t_cdhdr BY objectid ASCENDING
                      udate    DESCENDING.
      DELETE ADJACENT DUPLICATES FROM t_cdhdr COMPARING objectid.
    ENDIF.
  ENDIF.