Two select loops into single SELECT? - abap

I have been learning abap recently and working on select operations but then I came across this question. How can I put these 2 select statements into single select statement?
SELECT * FROM SPFLI INTO SPFLI_WA.
SELECT * FROM SFLIGHT INTO SFLIGHT_WA
WHERE CARRID = SPFLI_WA-CARRID
AND CONNID = SPFLI_WA-CONNID.
ENDSELECT.
ENDSELECT.

You can use inner join for getting matching records from other table.
SELECT SFLIGHT~*
INTO #SFLIGHT_WA
FROM SFLIGHT
INNER JOIN SPFLI ON SFLIGHT~CARRID = SPFLI~CARRID
AND SFLIGHT~CONNID = SPFLI~CONNID.
ENDSELECT.

Here is an extension of correct answer by mkysoft, just for the fun, to mimic exactly the original code ; it works from ABAP 7.40:
SELECT *
INTO #DATA(spfli_sflight_wa)
FROM spfli
INNER JOIN sflight ON sflight~carrid = spfli~carrid
AND sflight~connid = spfli~connid.
DATA(spfli_wa) = spfli_sflight_wa-spfli.
DATA(sflight_wa) = spfli_sflight_wa-sflight.
...
ENDSELECT.

Related

DB2 SQL for Join: Duplicate Table Designator B Error

I have a ColdFusion query like this:
SELECT a.Description, b.MCDL01
FROM ( SELECT GMDL01 as Description from F0901_LB where GMMCU = '950ALDA77') a, F0006_LA b
Inner Join b on a.GMMCU = b.MCMCU
Basically, the value of GMMCU in F0901_LB table exists as MCMCU in F0006_LA table. And I need to grab all matching MCDL01 from the F0006_LA table. But my join above gives error: Duplicate table designator B not valid.
BTW, I am not fully sure an Inner Join is needed but trying it to see what happens.
Any ideas?
Thanks.
You have two different syntax for joins here you want this (preferred):
SELECT a.Description, b.MCDL01
FROM ( SELECT GMDL01 as Description, GMMCU
from F0901_LB
where GMMCU = '950ALDA77') a
Inner Join F0006_LA b on a.GMMCU = b.MCMCU
this is the old way of doing it which you might have seen, but is not the best way to do it:
SELECT a.Description, b.MCDL01
FROM ( SELECT GMDL01 as Description, GMMCU
from F0901_LB
where GMMCU = '950ALDA77') a, F0006_LA b
where a.GMMCU = b.MCMCU
Just do it?
SELECT a.MCDL01, b.GMDL01 Description
FROM F0006_LA a inner join F0901_LB b on a.MCMCU = b.GMMCU and b.GMMCU = '950ALDA77'

Not able to LEFT OUTER JOIN three tables

I've got a problem.
Currently I have three tables, the first table is the main table and if there is no record in the second table, write it too. But if a record exists in the second table, so from the third table display "dispo" information..
I want to use three SAP table - lagp, lqua and marc.
Me goal is write all stock positions from lagp.
2x LEFT JOIN doesnt work: "Unable to compare with"B~MATNR". A table can be joined with a maximum of one other table usign LEFT OUTER JOIN.
Structure:
TYPES:
BEGIN OF t_work,
lgnum TYPE lgnum,
lgtyp TYPE lgtyp,
lgpla TYPE lgpla,
bdatu TYPE lagp_bdatu,
matnr TYPE matnr,
verme TYPE lqua_verme,
meins TYPE meins,
dispo TYPE dispo,
END OF t_work.
DATA:
lt_work TYPE TABLE OF t_work INITIAL SIZE 0,
ls_work LIKE LINE OF lt_work.
And SQL command:
SELECT a~lgnum a~lgtyp a~lgpla a~bdatu b~matnr b~verme b~meins c~dispo FROM lagp AS a
LEFT JOIN lqua AS b ON a~lgnum = b~lgnum AND a~lgtyp = b~lgtyp AND a~lgpla = b~lgpla
INNER JOIN marc AS c ON b~matnr = c~matnr AND b~werks = c~werks
INTO TABLE lt_work
WHERE a~lgnum IN so_lgnum
AND a~lgtyp IN so_lgtyp
AND a~skzua EQ space
AND a~skzue EQ space.
But as result is only one stock position - http://i.stack.imgur.com/1sEEo.png
Can you tell me, how the SQL code has look?
Thank you
Try:
SELECT a~lgnum, a~lgtyp, a~lgpla, a~bdatu, b~matnr, b~verme, b~meins, c~dispo
FROM lagp AS a
LEFT JOIN ( lqua AS b
JOIN marc AS c ON b~matnr = c~matnr AND b~werks = c~werks )
ON a~lgnum = b~lgnum AND a~lgtyp = b~lgtyp AND a~lgpla = b~lgpla
INTO TABLE #lt_work
WHERE a~lgnum IN #so_lgnum
AND a~lgtyp IN #so_lgtyp
AND a~skzua EQ #space
AND a~skzue EQ #space.
Other option would be to put the join between LQUA and MARC in a view and do an outer join with the view. Or split out the select on MARC and do that while looping at the data found with a select on LAGP and LQUA.
I got that fine working code:
SELECT ma~matnr ma~mtart ma~ernam ma~ersda ma~laeda
de~maktx as maktx_de fr~maktx as maktx_fr it~maktx as maktx_it
FROM mara as ma
LEFT JOIN MAKT as de ON de~matnr = ma~matnr AND de~spras = 'DE'
LEFT JOIN MAKT as fr ON fr~matnr = ma~matnr AND fr~spras = 'FR'
LEFT JOIN MAKT as it ON it~matnr = ma~matnr AND it~spras = 'IT'
INTO CORRESPONDING FIELDS OF TABLE g_it_material
WHERE ma~ernam IN so_ERNAM
AND ma~laeda IN so_LAEDA
AND ma~matnr IN so_MATNR.
and it works fine. What did you say about multi-leftjoins?

Error message - Every derived table must have its own alias

I have this SQL Syntax but it's not working and receive this error:
"#1248 - Every derived table must have its own alias".
Could you help me?
SELECT *
FROM produse_comenzi
JOIN comenzi ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi)
ON facturi.id_comanda = comenzi.id_comanda
In the second join you are using a subquery but you haven't given the result an alias, i.e. something to identify the result by
SELECT *
FROM produse_comenzi
JOIN comenzi
ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi) -- has no alias
ON facturi.id_comanda = comenzi.id_comanda
you should do
SELECT *
FROM produse_comenzi
JOIN comenzi
ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura, id_comanda FROM facturi) AS facturi
ON facturi.id_comanda = comenzi.id_comanda
You must add an alias to each subquery that's being treated as a table:
SELECT *
FROM produse_comenzi
JOIN comenzi ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi) x
ON x.id_comanda = comenzi.id_comanda
Here I have named the result set x and referred to that in the join condition. You can change "x" to whatever you like.
This should fix it:
(there is a need in SQL to distinguish between different Resultset from selects)
SELECT *
FROM produse_comenzi AS table_1
JOIN comenzi AS table_2
ON table_2.id_comanda = table_1.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi AS table_3)
ON table_3.id_comanda = table_2.id_comanda

Need to understand multiple joins correctly

I was trying to join 3 tables - CurrentProducts, SalesInvoice and SalesInvoiceDetail. SalesInvoiceDetail contains FK/foreign key to the other two tables and some other columns. The first query is ok but the second is not. My question comes at the end of the code.
Right
select *
from CurrentProducts inner join
(dbo.SalesInvoiceDetail inner join dbo.SalesInvoice
on dbo.SalesInvoiceDetail.InvoiceID = dbo.SalesInvoice.InvoiceID
)
on dbo.SalesInvoiceDetail.ProductID = dbo.CurrentProducts.ProductID
Wrong
select *
from CurrentProducts inner join
(select * from
dbo.SalesInvoiceDetail inner join dbo.SalesInvoice
on dbo.SalesInvoiceDetail.InvoiceID = dbo.SalesInvoice.InvoiceID
)
on dbo.SalesInvoiceDetail.ProductID = dbo.CurrentProducts.ProductID
error - Incorrect syntax near the keyword 'on'.
Why is the second query wrong ? Isn't it conceptually the same as the first one ? That is inside join makes a result set. We select * the result set and then join this result set to CurrentProducts ?
The first query is a "plain" join expressed with an older syntax. It can be rewritten as:
select
*
from
CurrentProducts
inner join dbo.SalesInvoiceDetail
on dbo.SalesInvoiceDetail.ProductID = dbo.CurrentProducts.ProductID
inner join dbo.SalesInvoice
on dbo.SalesInvoiceDetail.InvoiceID = dbo.SalesInvoice.InvoiceID
The second query is a join where the second table is a subquery. When you join on a subquery, you must assign an alias to it and use that alias to refer to the columns returned by the subquery:
select
*
from
CurrentProducts
inner join (select *
from dbo.SalesInvoiceDetail
inner join dbo.SalesInvoice
on SalesInvoiceDetail.InvoiceID = SalesInvoice.InvoiceID
) as foo on foo.ProductID = dbo.CurrentProducts.ProductID
You need to alias the inner query. Also, in the first one the parentheses are not needed.
select *
from CurrentProducts inner join
(select * from
dbo.SalesInvoiceDetail inner join dbo.SalesInvoice
on dbo.SalesInvoiceDetail.InvoiceID = dbo.SalesInvoice.InvoiceID
) A
on A.ProductID = dbo.CurrentProducts.ProductID

Left Join on Same table - Not recognizing nested SELECT statement

I am trying to pull two different values based on different criteria from the same table and in my Left Join of the same table it is not recognizing the SELECT statement.
The error is as follows:
Dynamic SQL Error
SQL error code = -104
Token unknown - line 7, char -1
SELECT.
The SQL Statement:
SELECT
b.dept,b.typ,c.brand,c.style,c.ext,c.description,
max(c.price),max(c.last_cost),sum(c.quan) "TOTAL INV",D.QUAN "WEB INV"
FROM
invt c
left outer join (
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
FROM invt WHERE store in ('997')
group by dept,typ,brand,style,ext,description) d
on (b.store = d.store and b.style = d.style and b.brand = d.brand)
LEFT OUTER JOIN
sku b
on c.style = b.style and c.brand = b.brand
where c.quan <> 0 or c.ord <> 0
GROUP BY
b.dept,b.typ,c.brand,c.style,c.ext,c.description
Try changing this line:
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
to this:
SELECT store,dept,typ,brand,style,ext,description,sum(quan) as quan
You do not need the d alias here.
UPDATE:
As #Jeremy Holovacs mentioned, you also seem to be using d.store for your join but it does not exist in your subquery.