-1

I want to build an OpenSQL query to return only the max stat value for all objnr.
This works:

SELECT O.OBJNR
  FROM JCDS AS O
  WHERE O.OBJNR = 'Obj12345'
  AND STAT = ( SELECT MAX(STAT) 
                 FROM JCDS AS I 
                 WHERE I.OBJNR = O.OBJNR )

However, if I try to use a join, and not provide objnr directly, I get an error message:

SELECT O.OBJNR, O.STAT
  FROM JCDS AS O
  INNER JOIN AFVC 
      ON AFVB.OBJNR = O.OBJNR
  WHERE "AUFPL" = 'Aufpl12345' 
  AND O.STAT = ( SELECT MAX(STAT) 
                   FROM JCDS AS I 
                   WHERE I.OBJNR = O.OBJNR )

The error message is meaningless; "SQL error"


EDIT 2023: the original question was tagged (along with ), and contained . after the aliases (not OpenSQL), hence the confusion in the answers. The code which was said "to work" had an error too. The author later revised the question to replace all . with ~, so the question was about OpenSQL for sure. was probably mistaken with (contains the tables JCDS and AFVC). Based on OP's answer (the only problem was a typo of AFVB instead of AFVC), I edited the question so that the original one is kept to comply with the original answers, and the below part is added to comply with the OP question and answer. I hope this edit brings clarity, and doesn't betray what the OP meant to say. I checked the code "this works" below, using ABAP/OpenSQL 7.52 (it was not the OP version for sure) and S/4HANA 1709.

I want to build an OpenSQL query in S/4HANA system to return only the max stat value for all objnr.

This works:

    SELECT O~OBJNR
      FROM JCDS AS O
      WHERE O~OBJNR = 'Obj12345'
      AND STAT = ( SELECT MAX( STAT ) 
                     FROM JCDS AS I 
                     WHERE I~OBJNR = O~OBJNR )
      INTO TABLE @DATA(result).

However, if I try to use a join, and not provide objnr directly, I get an error message:

    SELECT O~OBJNR, O~STAT
      FROM JCDS AS O
      INNER JOIN AFVC 
          ON AFVB~OBJNR = O~OBJNR
      WHERE AUFPL = 'Aufpl12345' 
      AND O~STAT = ( SELECT MAX( STAT ) 
                       FROM JCDS AS I 
                       WHERE I~OBJNR = O~OBJNR )
      INTO TABLE @DATA(result).

The error message is "AFVB" is unknown in the current FROM clause (obscured by an alias name or unknown in this position) and the cursor points on AFVB.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
András
  • 1,326
  • 4
  • 16
  • 26
  • could you please post the error message that thrown to you – Zaynul Abadin Tuhin Oct 02 '18 at 16:27
  • I deleted my comment, hope it is expected as an answer :) – Eralper Oct 04 '18 at 10:36
  • can you confirm your question was about `Open SQL`, because your syntax with `.` after table aliases is invalid for Open SQL, you should use `~` instead (also other syntax issues). – Sandra Rossi Jun 15 '19 at 07:24
  • Removing [tag:hana] as the OP has replaced `.` with `~` in the code of the question after my comment, which proves that it was intended to be an OpenSQL question (I guess the OP meant S/4HANA. As per OP's answer, I edit the question so that to make both the question and the answers coherent. – Sandra Rossi Jan 20 '23 at 18:22

4 Answers4

1

you could simplify your query like this by grouping by the objectnumbers:

SELECT O.OBJNR, MAX(O.STAT)
FROM JCDS AS O
GROUP BY O.OBJNR

Depending on which columns you need to display from you joined tables you could either extend the group by:

SELECT O.OBJNR, A.SOMECOL, MAX(O.STAT)
FROM JCDS AS O
INNER JOIN AFVC AS A
ON AFVB.OBJNR = O.OBJNR
GROUP BY O.OBJNR, A.SOMECOL

or use a subquery:

SELECT MAXSTAT.OBJNR, MAXSTAT.STAT, A.SOMECOL
FROM (
SELECT O.OBJNR, MAX(O.STAT) STAT
FROM JCDS AS O
GROUP BY O.OBJNR ) MAXSTAT
INNER JOIN AFVC AS A
ON AFVB.OBJNR = MAXSTAT.OBJNR
1

To answer the question after EDIT 2023 (question about OpenSQL / ABAP SQL), in short, it's a typo: instead of AFVB~OBJNR, it should be AFVC~OBJNR. This code compiles in S/4HANA 1709 ABAP 7.52:

SELECT O~OBJNR, O~STAT
  FROM JCDS AS O 
  INNER JOIN AFVC 
    ON afvC~OBJNR = O~OBJNR 
  WHERE AUFPL = '0123456789' 
    AND O~STAT = ( SELECT MAX( STAT ) 
                     FROM JCDS AS I 
                     WHERE I~OBJNR = O~OBJNR )
  INTO TABLE @DATA(result).

NB:

  • The spaces are mandatory in MAX( STAT )
  • INTO ... is mandatory
  • The double quotes in "AUFPL" = '0123456789' are invalid in ABAP SQL (double quotes are used for defining comments in ABAP)
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
0

Are you looking for the record with the highest stat per objnr? You can use a window function for this:

select *
from
(
  select
    jcds.*,
    max(stat) over (partition by objnr) as max_stat
  from jcds
) data
where stat = max_stat;
Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73
-1

The subselect works flawlessly with a JOIN, if I do not mistype the tablename:

    SELECT O~OBJNR, O~STAT
      FROM JCDS AS O
      INNER JOIN AFVC 
    --    ON afvB~OBJNR = O~OBJNR
    --          |
          ON afvC~OBJNR = O~OBJNR
      WHERE AUFPL = '0123456789' 
      AND O~STAT = ( SELECT MAX(STAT) 
                       FROM JCDS AS I 
                       WHERE I~OBJNR = O~OBJNR )
András
  • 1,326
  • 4
  • 16
  • 26
  • OpenSQL `WHERE "AUFPL" = 'Aufpl12345'` doesn't compile. – Sandra Rossi Jan 20 '23 at 18:19
  • Still doesn't compile. Lots of syntax errors (-- and double quotes not permitted, spaces and resulting variable required). For instance, this one compiles in S/4HANA 1709 ABAP 7.52: `SELECT O~OBJNR, O~STAT FROM JCDS AS O INNER JOIN AFVC ON afvC~OBJNR = O~OBJNR WHERE AUFPL = '0123456789' AND O~STAT = ( SELECT MAX( STAT ) FROM JCDS AS I WHERE I~OBJNR = O~OBJNR ) INTO TABLE @DATA(result).` – Sandra Rossi Jan 23 '23 at 07:46
  • 1
    @SandraRossi, I tried to delete the question, but cannot. Just forget about it – András Jan 23 '23 at 15:24