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

joininig SQL statement

Former Member
0 Likes
1,778

Hi all,

I want to join 2 db tables (A, B), and select a minimum column (A~field1) from db table A, what should be the sql statement? Thanks.

Select (....)

into corresponding fields of itab

from ekko as A

join ekpo as B

on Aebeln = Bebeln

where (....).

append itab.

Endselect

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,746

DATA : begin of i_data occurs 0,
          field1 LIKE ekko-field1,
          field2 LIKE ekpo-field2,
          menge LIKE eket-menge,
       end of i_data.

Select ekko~field1 ekpo~field2
min( eket~menge )
into  TABLE i_data
from ekko
join ekpo
on ekko~ebeln = ekpo~ebeln
join eket
on ekpo~ebeln = eket~ebeln and ekpo~ebelp = eket~ebelp
group by ekko~field1 ekpo~field2

Check whether it works and please let us know.

17 REPLIES 17
Read only

suresh_datti
Active Contributor
0 Likes
1,746

Hi Macy,

try this..

Select (....)

into corresponding fields of table itab

from ekko as A

inner join ekpo as B

on Aebeln = Bebeln

where (....).

Regards,

Suresh Datti

Read only

Former Member
0 Likes
1,746
Select (....)
into corresponding fields of itab
from ekko as A
join ekpo as B
on A~ebeln = B~ebeln
where A~field1 = ( SELECT min(field1) from A ).

append itab. 
Endselect

Hope it works...

Read only

former_member186741
Active Contributor
0 Likes
1,746

Your prototype select is pretty much ok, you need the MIN aggregate function:

Select MIN (....) AS ??? "??? = SAME NAME AS COLUMN IN ITAB

into corresponding fields of TABLE itab

from ekko as A

join ekpo as B

on Aebeln = Bebeln

where (....).

Message was edited by: Neil Woodruff

Read only

0 Likes
1,746

Sorry,

I should have said that I have 3 db tables (A, B, C) to join.

And I'd like to get the min fieldX value from C.

What should be the SQL statement?

Select Afield1 Bfield2

min( C~fieldX)

into corresponding fields of TABLE itab

from ekko as A

join ekpo as B

on Aebeln = Bebeln

join eket as C

on Bebeln = Cebeln and Bebelp = Cebelp

where (....).

append itab.

Endselect.

Read only

0 Likes
1,746

Select Afield1 Bfield2

min( C~fieldX) as fieldX

into corresponding fields of TABLE itab

from ekko as A

join ekpo as B

on Aebeln = Bebeln

join eket as C

on Bebeln = Cebeln and Bebelp = Cebelp

where (....).

  • no append or endselect required

Read only

0 Likes
1,746

Thanks.

I've followed the code.

But there is syntax error:

The field "BMEINS" from the SELECT list is missing in the GROUP BY clause. addition INTO wa or INTO (g1,...gn) is required. fields of type "" or "AMANDT".

Read only

0 Likes
1,746

try again, for example:

Select Aebeln Bebelp c~etenr

min( C~eindt ) as eindt

into corresponding fields of table tt

from ekko as A

join ekpo as B

on Aebeln = Bebeln

join eket as C

on Bebeln = Cebeln and Bebelp = Cebel

group by aebeln bebelp c~etenr

.

Read only

Former Member
0 Likes
1,746

This works ...

TABLES : ekko.

DATA : begin of i_data occurs 0,
          ebeln LIKE ekko-ebeln,
       end of i_data.

select ekko~ebeln
  into table i_data
  from ekko join ekpo on ekko~ebeln = ekpo~ebeln
where ekko~field1 IN ( SELECT min( field1 ) from ekko ).

Is this what you want?

Read only

Former Member
0 Likes
1,746
Select ekko~field1 ekpo~field2 
min( eket~field3 )
into corresponding fields of TABLE itab
from ekko
join ekpo
on ekko~ebeln = ekpo~ebeln
join eket
on ekpo~ebeln = eket~ebeln and ekpo~ebelp = eket~ebelp 
group by ekko~field1 ekpo~field2
Read only

Former Member
0 Likes
1,747

DATA : begin of i_data occurs 0,
          field1 LIKE ekko-field1,
          field2 LIKE ekpo-field2,
          menge LIKE eket-menge,
       end of i_data.

Select ekko~field1 ekpo~field2
min( eket~menge )
into  TABLE i_data
from ekko
join ekpo
on ekko~ebeln = ekpo~ebeln
join eket
on ekpo~ebeln = eket~ebeln and ekpo~ebelp = eket~ebelp
group by ekko~field1 ekpo~field2

Check whether it works and please let us know.

Read only

0 Likes
1,746

Then where could I put the "where clause" ?

Read only

0 Likes
1,746

Select Aebeln Bebelp c~etenr

min( C~eindt ) as eindt

into corresponding fields of table tt

from ekko as A

join ekpo as B

on Aebeln = Bebeln

join eket as C

on Bebeln = Cebeln and Bebelp = Cebel

where .......

group by aebeln bebelp c~etenr

.

Read only

0 Likes
1,746

So many thanks to all of you.

It works. But the result in the itab is unexpected.

I want to retrieve the entry with minimum eindt into table itab, but all the entry (not only min.) are retrieved into itab.

Read only

0 Likes
1,746

Hi Macy,

this sql gives you the minimun eindt for each ETENR not the MIN for the whole set. What is it that you wanted?

Read only

0 Likes
1,746

Hi Neil,

I want to get 1 entry with its minimum delivery date MIN( EINDT ) and other info for each PO item

Read only

0 Likes
1,746

then try,

Select Aebeln Bebelp c~etenr

C~eindt as eindt

into corresponding fields of table tt

from ekko as A

join ekpo as B

on Aebeln = Bebeln

join eket as C

on Bebeln = Cebeln and Bebelp = Cebelp

where eindt IN ( SELECT min( eindt ) from eket )

.

But, this gives you the lowest EINDT in the whole EKET table which is not necessarily what you want. I think you want the MIN (EINDT) of the ones you have selected but I'm not sure how to achieve that.

Message was edited by: Neil Woodruff

Read only

Former Member
0 Likes
1,746

If you want the minimum value of menge for ebeln

Then use...

Select ekko~ebeln
min( eket~menge )
into corresponding fields of TABLE i_data
from ekko
join ekpo
on ekko~ebeln = ekpo~ebeln
join eket
on ekpo~ebeln = eket~ebeln and ekpo~ebelp = eket~ebelp
where ....
group by ekko~ebeln.