‎2020 Jul 27 10:06 AM
Hi, all
I have a table with following fields:
FIELD1 KEY
FIELD2 KEY
FIELD3 (no key but secondary key exists)
DATE
TIME
I would like to select all the records with last date and time grouping only by FIELD2.
I'm trying the following:
SELECT FIELD1 FIELD2 FIELD3 MAX( DATA ) MAX( TIME )
FROM MY_TABLE
WHERE FIELD3 EQ 'TEST'
GROUP BY FIELD2.
I have the following sintax error:
The field "FIELD1" from the SELECT list is is missing in the GROUP BY clause.
But I dont want to group by FIELD1, because can have different values for each FIELD2. I only want to get all the colums for each FIELD2 that have the max date and time.
Is it possible with a single select?
ABAP: 731
Thanks
‎2020 Jul 27 10:08 AM
why do you specify columns FIELD1, FIELD3 if you only want FIELD2 ?
‎2020 Jul 27 10:36 AM
Please format your code with the button CODE.
Something like this?
SELECT CARRID, CONNID, FLDATE
FROM SFLIGHT AS A
WHERE EXISTS (
SELECT B~CARRID FROM SFLIGHT AS B
WHERE A~CARRID = B~CARRID
AND A~CONNID = B~CONNID
GROUP BY B~CARRID
HAVING A~FLDATE = MAX( B~FLDATE ) )
INTO TABLE @DATA(SFLIGHT_LINES).
‎2020 Jul 27 11:05 AM
‎2020 Jul 27 11:22 AM
Thanks Sandra. I think this is what I need.
Freederic, I want all the columns, but only group by FIELD2
‎2020 Jul 27 11:43 AM
oliver.am, so I think the solution of sandra.rossi is the good one.
First select to get all the key needed, second, get the final result.
But, what would be the result if you have
Field1 Field2 Date
A A 01.01.2010
B A 02.01.2010
C A 02.01.2010
Because first select give Field 2: A Date: 02.01.2010
But field1 will have two possible values
‎2020 Jul 27 3:05 PM
Hi, Frederic.
That's a good question, I guess depends on the order of the rows.
I'll check with functional team what we need to do in this case.
MAX is returning only one of these rows? or both with the MAX value?
‎2020 Jul 27 7:15 PM
If you want to target someone, if this person has posted an Answer, use the button REPLY, if this person is the Original Poster of the question he/she will be automatically informed, otherwise copy/paste their hyperlinked name so that the person receives a warning (NB: @ doesn't work/but typing this character will suggest hyperlinked names).