2023 Feb 19 7:12 AM
Dear all seniors,
I have a requirement to pick some fields (Matnr, Menge, Meins) from the table "MSEG" on the basis of two fields of MKPF Table, which are common in both tables (MBLNR & MJAHR) so i have used where clause there is no problem till there.
But Now i have to pick 1 field from MAKT which is MAKT-MAKTX, now i am confused in logic to pick that field from MAKT table.
Queries that i am using are,
For MKPF: SELECT SINGLE budat usnam blart FROM mkpf
INTO wa_mkpf
WHERE mblnr = mkpf-mblnr AND
mjahr = mkpf-mjahr.
For MSEG: SELECT SINGLE budat usnam blart FROM mkpf
INTO wa_mkpf
WHERE mblnr = mkpf-mblnr AND
mjahr = mkpf-mjahr.
2023 Feb 19 9:11 AM
Do a single select with JOIN with the 3 select
select MKPF-BUDAT, MKPF-USNAM, MKPF-BLART,
MSEG-MATNR, MSEG-MENGE, MSEG-MEINS,
MAKT-MAKTX
from MSEG
join MKPF on MSEG-MBLNR = MKPF-MBLNR and MSEG-MJAHR = MKPF-MJAHR
left join MAKT on MAKT-MATNR = MSEG-MATNR AND MAKT-SPRAS = SY-LANGU
where MKPF-MBLNR = P_MBLNR AND MKPF-MJAHR = P_JAHR.
(add the language key to prevent duplicates)
2023 Feb 19 8:24 AM
zaibshah-
You should join MAKT on MATNR field:
select MSEG-MATNR,MSEG-MENGE,MSEG-MEINS,MAKT-MAKTX
from MSEG
inner join MKPF on MSEG-MBLNR = MKPF-MBLNR and MSEG-MJAHR = MKPF-MJAHR
inner join MAKT on MAKT-MATNR = MSEG-MATNR
where MKPF-MBLNR = '4500002100'
2023 Feb 20 7:02 AM
2023 Feb 19 9:11 AM
Do a single select with JOIN with the 3 select
select MKPF-BUDAT, MKPF-USNAM, MKPF-BLART,
MSEG-MATNR, MSEG-MENGE, MSEG-MEINS,
MAKT-MAKTX
from MSEG
join MKPF on MSEG-MBLNR = MKPF-MBLNR and MSEG-MJAHR = MKPF-MJAHR
left join MAKT on MAKT-MATNR = MSEG-MATNR AND MAKT-SPRAS = SY-LANGU
where MKPF-MBLNR = P_MBLNR AND MKPF-MJAHR = P_JAHR.
(add the language key to prevent duplicates)
2023 Feb 20 7:01 AM
2023 Feb 20 8:33 AM
zaibshah- You have marked the other answer as accepted, so I'm not sure you understood well: better use
left join MAKT on MAKT-MATNR = MSEG-MATNR AND MAKT-SPRAS = SY-LANGU
rather than
inner join MAKT on MAKT-MATNR = MSEG-MATNR
because of language selection, and also because for texts left join is better than inner join in case there's no text provided in the database.