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 MAX(...) ... INTO TABLE Whats wrong with this ?

Former Member
0 Likes
54,498

Hi,

SELECT - aggregate with max:

Selecting max from an database table from a group of rows into an internal table :

Whats wrong with this ?

DATA: t_table TYPE TABLE OF /BIC/t_table.

SELECT column_a

MAX( column_b)

FROM /BIC/t_table.

INTO TABLE t_table

GROUP BY column_a.

ST22 gives: DBIF_RSQL_INVALID_RSQ

ThanXs

Martin

1 ACCEPTED SOLUTION
Read only

Former Member
18,428

Hi Martin,

you need to specify target for MAX( column_b).

try this:

types: begin of ty_tab,
        column_a type SOME_TYPE,
        max type some_type,
       end of ty_tab.

data t_table TYPE TABLE OF ty_tab.

SELECT column_a
MAX( column_b ) as max
FROM /BIC/t_table
INTO TABLE t_table
GROUP BY column_a.

regards

rea

Hi,

SELECT - aggregate with max:

Selecting max from an database table from a group of rows into an internal table :

Whats wrong with this ?

DATA: t_table TYPE TABLE OF /BIC/t_table.

SELECT column_a

MAX( column_b)

FROM /BIC/t_table.

INTO TABLE t_table

GROUP BY column_a.

ST22 gives: DBIF_RSQL_INVALID_RSQ

ThanXs

Martin

5 REPLIES 5
Read only

Former Member
0 Likes
18,428

Martin,

try this

DATA: t_table TYPE TABLE OF /BIC/t_table.

SELECT column_a

MAX( column_b)

FROM /BIC/t_table.

INTO corresponding fields of TABLE t_table

GROUP BY column_a.

Thanks

Bala Duvvuri

Read only

Former Member
18,429

Hi Martin,

you need to specify target for MAX( column_b).

try this:

types: begin of ty_tab,
        column_a type SOME_TYPE,
        max type some_type,
       end of ty_tab.

data t_table TYPE TABLE OF ty_tab.

SELECT column_a
MAX( column_b ) as max
FROM /BIC/t_table
INTO TABLE t_table
GROUP BY column_a.

regards

rea

Read only

0 Likes
18,428

very good answer, I was juggling with this for a while.

Read only

Former Member
0 Likes
18,428

erm guys isnt this obvious or am i just dumbing down something that seems clear to me?

Select MAX gets you the maximum value of that field over the whole table.

So Select MAX gets you ONE value.

it doens make sense to select one value into a table.

i would do follwwing:


DATA: t_table TYPE TABLE OF /BIC/t_table,
            lv_max type kbetr,
            wa_table type /bic/_t_table.

SELECT column_a
FROM /BIC/t_table.
INTO TABLE t_table
GROUP BY column_a.

SELECT MAX( column_b)
FROM /BIC/t_table.
INTO lv_max.

loop at t_table into wa_table.
  t_table-column_b = lv_max.
  modify t_table from wa_table index sy-tabix.
endloop.

Read only

0 Likes
18,428

Hi Florian,

SELECT column_a
MAX( column_b)
[...]
GROUP BY column_a.

above sql statement returns max value of column b grouped by column a - so result could be more than one line depending on values found in column a.

regards

rea