‎2007 May 16 9:09 PM
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?
‎2007 May 16 9:13 PM
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
‎2007 May 16 9:14 PM
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
‎2007 May 16 9:16 PM
You can try by selecting all the records, Then sort the table and then delete the rest else the MAX.
Shreekant
‎2007 May 16 9:13 PM
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
‎2007 May 16 9:16 PM
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
‎2007 May 16 9:30 PM
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.
‎2007 May 16 9:33 PM
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
‎2007 May 16 9:36 PM
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.
‎2007 May 16 9:39 PM
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.