2006 Jan 17 3:01 AM
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
2006 Jan 17 3:43 AM
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~field2Check whether it works and please let us know.
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
2006 Jan 17 3:05 AM
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
2006 Jan 17 3:05 AM
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...
2006 Jan 17 3:05 AM
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
2006 Jan 17 3:21 AM
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.
2006 Jan 17 3:23 AM
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
2006 Jan 17 3:38 AM
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".
2006 Jan 17 3:48 AM
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
.
2006 Jan 17 3:17 AM
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?
2006 Jan 17 3:30 AM
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
2006 Jan 17 3:43 AM
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~field2Check whether it works and please let us know.
2006 Jan 17 3:49 AM
2006 Jan 17 3:50 AM
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
.
2006 Jan 17 4:08 AM
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.
2006 Jan 17 4:10 AM
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?
2006 Jan 17 4:16 AM
Hi Neil,
I want to get 1 entry with its minimum delivery date MIN( EINDT ) and other info for each PO item
2006 Jan 17 4:24 AM
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
2006 Jan 17 4:19 AM
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.
| User | Count |
|---|---|
| 6 | |
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |