I'm getting a syntax error pointing to my FROM clause, but I can't figure it out. The goal here is to use subqueries to filter and return some information before joining.
What I've tried: This question on SO suggested that I surround the 2 LEFT JOINS with parentheses. I did so, but it didn't help. Below is the result, and the error I get is in the title.
It may or may not be important to note that before doing the parentheses around both JOINS, I was getting the following error: Syntax error (missing operator in query expression 'tbl_HersheySAPInventory.item = maxdatejob.item LEFT JOIN (SELECT * FROM dbo_Item_by_Location_LP_All WHERE qty_on_hand >'.
SELECT
tbl_HersheySAPInventory.ID,
tbl_HersheySAPInventory.item,
dbo_job.job_date,
dbo_job.job,
dbo_job.suffix,
Sum(comptable.qty_on_hand) AS SumOfqty_on_hand,
comptable.whse,
comptable.product_code
FROM
tbl_HersheySAPInventory
(LEFT JOIN
(SELECT
dbo_job.job_date,
dbo_job.job,
dbo_job.suffix,
dbo_job.item AS FG,
dbo_jobmatl.item
FROM
dbo_job
INNER JOIN dbo_jobmatl ON dbo_job.job = dbo_jobmatl.job AND dbo_job.suffix = dbo_jobmatl.suffix
WHERE
dbo_job.item Not Like "Indirect" AND
dbo_job.job Not Like "C0*" AND
dbo_job.job Not Like "*R0*"
) AS maxdatejob
ON tbl_HersheySAPInventory.item = maxdatejob.item)
(LEFT JOIN
(SELECT * FROM dbo_Item_by_Location_LP_All WHERE qty_on_hand > 0 AND whse = Forms!MainForm!Combo353) AS comptable
ON comptable.item = tbl_HersheySAPInventory.item)
ORDER BY
tbl_HersheySAPInventory.item,
dbo_job.job_date;
You may have a problem with your parentheses. I have similar issues writing SQL into Access - there seem to be some strange requirements wrapping parens around JOINS and WHERE clauses.
The pseudocode that I recommend trying is:
FROM
(
(
Table A
)
LEFT JOIN
(
Subquery1
)
)
LEFT JOIN
(
Subquery2
)
I've taken a stab at updating below. Give this a try and let me know:
SELECT
tbl_HersheySAPInventory.ID,
tbl_HersheySAPInventory.item,
dbo_job.job_date,
dbo_job.job,
dbo_job.suffix,
comptable.whse,
comptable.product_code,
Sum(comptable.qty_on_hand) AS SumOfqty_on_hand
FROM
(
(
tbl_HersheySAPInventory
LEFT JOIN
(SELECT
dbo_job.job_date,
dbo_job.job,
dbo_job.suffix,
dbo_job.item AS FG,
dbo_jobmatl.item
FROM
dbo_job
INNER JOIN dbo_jobmatl ON dbo_job.job = dbo_jobmatl.job AND dbo_job.suffix = dbo_jobmatl.suffix
WHERE
dbo_job.item Not Like "Indirect" AND
dbo_job.job Not Like "C0*" AND
dbo_job.job Not Like "*R0*"
) AS maxdatejob
ON tbl_HersheySAPInventory.item = maxdatejob.item
)
)
LEFT JOIN
(SELECT * FROM dbo_Item_by_Location_LP_All WHERE qty_on_hand > 0 AND whse = Forms!MainForm!Combo353) AS comptable
ON comptable.item = tbl_HersheySAPInventory.item
GROUP BY
tbl_HersheySAPInventory.ID,
tbl_HersheySAPInventory.item,
dbo_job.job_date,
dbo_job.job,
dbo_job.suffix,
comptable.whse,
comptable.product_code
ORDER BY
tbl_HersheySAPInventory.item,
dbo_job.job_date;
Related
EDIT:
The result supposed to be like this:
desired result
I have this query:
SELECT DISTINCT mitarbeiter.mitarbnr, mitarbeiter.login, mitarbeiter.name1, mitarbeiter.name2
FROM vertragspos
left join vertrag_ek_vk_zuord ON vertragspos.id = vertrag_ek_vk_zuord.ek_vertragspos_id
left join mitarbeiter ON vertrag_ek_vk_zuord.anlage_mitarbnr = mitarbeiter.mitarbnr
left join vertragskopf ON vertragskopf.id = vertragspos.vertrag_id
left join
(
SELECT wkurse.*, fremdwaehrung.wsymbol
FROM wkurse
INNER join
(
SELECT lfdnr, Max(tag) AS maxTag
FROM wkurse
WHERE tag < SYSDATE
GROUP BY lfdnr
) t1
ON wkurse.lfdnr = t1.lfdnr AND wkurse.Tag = t1.maxTag
INNER JOIN fremdwaehrung ON wkurse.lfdnr = fremdwaehrung.lfdnr
) wkurse ON vertragskopf.blfdwaehrung = wkurse.lfdnr
left join
(
SELECT vertrag_ID, Sum (preis) preis, Sum (menge) menge, Sum (preis * menge / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
GROUP BY vertrag_ID
) s ON vertragskopf.id = s.vertrag_id
But I always get an error on line 21 Pos 145:
ORA-00904 WKURSE.KURS invalid identifier
The WKURSE table is supposed be joined already above, but why do I still get error?
How can I do join with all these tables?
I need to join all these tables:
Mitarbeiter, Vertragspos, vertrag_ek_vk_zuord, wkurse, fremdwaehrung, vertragskopf.
What is the right syntax? I'm using SQL Tool 1,8 b38
Thank you.
Because LEFT JOIN is executed on entire dataset, and not in row-by-row manner. So there's no wkurse.kurs available in the execution context of subquery. Since you join that tables, you can place the calculation in the top-most select statement.
EDIT:
After you edited the statement, it became clear where does vertragskopf.zahlintervall came from. But I don't know where are you going to use calculated vertragswert (now it is absent in the query), so I've put it in the result. As I'm not a SQL parser and have no idea of your tables, so I cannot check the code, but calculation now can be resolved (all the values are available in calculation context).
SELECT DISTINCT mitarbeiter.mitarbnr, mitarbeiter.login, mitarbeiter.name1, mitarbeiter.name2, s.amount / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
left join vertrag_ek_vk_zuord ON vertragspos.id = vertrag_ek_vk_zuord.ek_vertragspos_id
left join mitarbeiter ON vertrag_ek_vk_zuord.anlage_mitarbnr = mitarbeiter.mitarbnr
left join vertragskopf ON vertragskopf.id = vertragspos.vertrag_id
left join (
SELECT wkurse.*, fremdwaehrung.wsymbol
FROM wkurse
INNER join (
SELECT lfdnr, Max(tag) AS maxTag
FROM wkurse
WHERE tag < SYSDATE
GROUP BY lfdnr
) t1
ON wkurse.lfdnr = t1.lfdnr AND wkurse.Tag = t1.maxTag
INNER JOIN fremdwaehrung ON wkurse.lfdnr = fremdwaehrung.lfdnr
) wkurse ON vertragskopf.blfdwaehrung = wkurse.lfdnr
left join (
SELECT vertrag_ID, Sum (preis) preis, Sum (menge) menge, Sum (preis * menge) as amount
FROM vertragspos
GROUP BY vertrag_ID
) s ON vertragskopf.id = s.vertrag_id
Rewriting the code using WITH clause makes it much clearer than select from select.
Also get the rate on last day before today in oracle is as simple as
select wkurse.lfdnr
, max(wkurse.kurs) keep (dense_rank first order by wkurse.tag desc) as rate
from wkurse
where tag < sysdate
group by wkurse.lfdnr
One option is a lateral join:
left join lateral
(SELECT vertrag_ID, Sum(preis) as preis, Sum(menge) as menge,
Sum (preis * menge / Decode (vertragskopf.zahlintervall, 1,1,2,2,3,3,4,6,5,12,1) / wkurse.kurs) vertragswert
FROM vertragspos
GROUP BY vertrag_ID
) s
ON vertragskopf.id = s.vertrag_id
Anybody know why this isn't working? I'm getting: ERROR: syntax error at or near "most_recent"
with most_recent as (SELECT MAX(public."Master_playlist".updated_at)
FROM public."Master_playlist")
SELECT * from public."Playlist"
JOIN public."Master_playlist_playlist" on public."Playlist".id = public."Master_playlist_playlist".playlist_id
JOIN public."Master_playlist" on public."Master_playlist_playlist".master_playlist_id = public."Master_playlist".id
WHERE public."Master_playlist".updated_at = most_recent;
Supposed to be getting the most recent date from Master_playlist and then using that to select a Master_playlist to join the inner query with
Thanks! HM
The with clause creates a derived table, which you need select from, using a join or a subquery. You also need to alias the column so you can refer to it afterwards, as in:
with most_recent as (
SELECT MAX(updated_at) max_updated_at
FROM public."Master_playlist"
)
SELECT *
from public."Playlist"
JOIN public."Master_playlist_playlist"
on public."Playlist".id = public."Master_playlist_playlist".playlist_id
JOIN public."Master_playlist"
on public."Master_playlist_playlist".master_playlist_id = public."Master_playlist".id
WHERE public."Master_playlist".updated_at = (SELECT max_updated_at FROM most_recent)
But here, it looks like it is simpler to use a row-limiting query:
select ...
from (
select *
from public."Master_playlist"
order by updated_at desc
limit 1
) mp
inner join public."Master_playlist_playlist" mpp
on mpp.master_playlist_id = mp.id
inner join public."Playlist" p
on p.id = mpp.playlist_id
I am trying to create a subquery (for a particular column) inside my base query. the code is as follows.
SELECT z.po_id,
max
(
select etcdc.ship_evnt_tms
FROM covinfos.shipment_event etcdc
WHERE etcdc.ship_evnt_cd = '9P'
AND etcdc.ship_id=scdc.ship_id
ORDER BY etcdc.updt_job_tms desc
FETCH first ROW only) AS llp_estimated_delivery_cdc
FROM covinfos.ibm_plant_order z
LEFT JOIN covinfos.ipo_line_to_case a
ON z.po_id = a.po_id
LEFT JOIN covinfos.shipment scdc
ON (
a.ship_id = scdc.ship_id
AND a.ship_to_loc_code = scdc.ship_to_loc_code
AND scdc.loc_type = 'CDC')
GROUP BY z.po_id
There seems to be some kind of typo somewhere based on the error message that pops up when I try to run the code.
BIC00004. DAL01008. An error occurred while accessing the database.
ILLEGAL SYMBOL ".". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: , ). SQLCODE=-104,
SQLSTATE=42601, DRIVER=3.62.56; THE CURSOR SQL_CURLH200C1 IS NOT IN A
PREPARED STATE. SQLCODE=-514, SQLSTATE=26501, DRIVER=3.62.56
However, at plain sight or at least my sight, there is nothing that spots the misstake. Furthermore, running the subselect in a blank sheet (outside the base query, new one) it does correctly.
Thanks
You would probably be best to remove the co-related sub-select, and just join to a plain sub-select. E.g.
SELECT z.po_id,
max(ship_evnt_tms) AS llp_estimated_delivery_cdc
FROM covinfos.ibm_plant_order z
LEFT JOIN covinfos.ipo_line_to_case a
ON z.po_id = a.po_id
LEFT JOIN covinfos.shipment scdc
ON a.ship_id = scdc.ship_id
AND a.ship_to_loc_code = scdc.ship_to_loc_code
AND scdc.loc_type = 'CDC'
LEFT JOIN
( select ship_id
, ship_evnt_tms
FROM
( select ship_id
, ship_evnt_tms
, row_number() over(partition by ship_id order by updt_job_tms desc) as RN
FROM covinfos.shipment_event
WHERE ship_evnt_cd = '9P'
) s
WHERE RN = 1
) AS etcdc
ON etcdc.ship_id=scdc.ship_id
GROUP BY z.po_id
P.S. you could just INNER JOINs unless you want to include po_id's with no ship_evnt_tms
Try adding parentheses around the sub-select. At least this then parses Data Studio using z/OS validation
SELECT z.po_id,
max
((
select etcdc.ship_evnt_tms
FROM covinfos.shipment_event etcdc
WHERE etcdc.ship_evnt_cd = '9P'
AND etcdc.ship_id=scdc.ship_id
ORDER BY etcdc.updt_job_tms desc
FETCH first ROW only)) AS llp_estimated_delivery_cdc
FROM covinfos.ibm_plant_order z
LEFT JOIN covinfos.ipo_line_to_case a
ON z.po_id = a.po_id
LEFT JOIN covinfos.shipment scdc
ON (
a.ship_id = scdc.ship_id
AND a.ship_to_loc_code = scdc.ship_to_loc_code
AND scdc.loc_type = 'CDC')
GROUP BY z.po_id
Still, I'm not sure this is a very nice bit of SQL code.
I have this join into large query
LEFT JOIN --RIVA MICRO
(SELECT
TT.DATAAREAID, TT.VOUCHER,MA.MAINACCOUNTID AS AD,
SUM(TT.TAXBASEAMOUNT) AS TAXBASEAMOUNT,
SUM(TT.TAXAMOUNT) AS TAXAMOUNT
FROM
TAXTRANS TT
INNER MERGE JOIN
TAXTRANSDETAIL_BE MA ON TT.RECID = MA.RECID
WHERE
TT.TRANSDATE <= #FECHA AND ("+#Codigo_RIVA+")
GROUP BY
TT.DATAAREAID, TT.VOUCHER, MA.MAINACCOUNTID) AS RIVA2 ON Asiento.Asiento = RIVA2.VOUCHER
AND Asiento.Cuenta = RIVA2.AD
AND Asiento.Empresa = RIVA2.DATAAREAID
It works correctly but now I implement a temporary table, so my query is like:
LEFT JOIN
(SELECT
DATAAREAID, VOUCHER, MAINACCOUNTID,
SUM(TAXBASEAMOUNT) AS TAXBASEAMOUNT, SUM(TAXAMOUNT)
FROM
##TTM TT
WHERE
(TT.TAXCODE LIKE 'RIVA11DPT' OR TT.TAXCODE LIKE 'RIVA16DPT'
OR TT.TAXCODE LIKE 'RIVA16T')
GROUP BY
TT.TAXAMOUNT, TT.TAXBASEAMOUNT, TT.TAXCODE, TT.DATAAREAID,
TT.VOUCHER, TT.MAINACCOUNTID) AS RIVA2 ON Asiento.Asiento = RIVA2.VOUCHER
AND Asiento.Cuenta = RIVA2.MAINACCOUNTID
AND Asiento.Empresa = RIVA2.DATAAREAID
But now problem is into GROUP BY CLAUSE, I´m getting into alias AS RIVA2
Incorrect syntax near the keyword 'AS'.
Someone knows why it occurs?. Regards
I think you intend:
LEFT JOIN
(SELECT TT.DATAAREAID, TT.VOUCHER, TT.MAINACCOUNTID,
SUM(TT.TAXBASEAMOUNT) AS TAXBASEAMOUNT, SUM(TT.TAXAMOUNT)
FROM ##TTM TT
WHERE TT.TAXCODE IN ('RIVA11DPT', 'RIVA16DPT', 'RIVA16T')
GROUP BY TT.DATAAREAID, TT.VOUCHER, TT.MAINACCOUNTID
) RIVA2
ON Asiento.Asiento = RIVA2.VOUCHER AND
Asiento.Cuenta = RIVA2.MAINACCOUNTID AND
Asiento.Empresa = RIVA2.DATAAREAID
Notes:
You only need to aggregate by the unaggregated columns in the GROUP BY.
It is well known that you cannot use column aliases defined in a SELECT in most other clauses associated with that SELECT.
IN is much simpler than a collection of OR clauses.
You should give SUM(TT.TAXAMOUNT) a name.
As written in the title: How to write this SQL statement correctly?
select
sl.switch_ip,
sl.switch_name,
count(m.switch_ip) as macentries,
(select arpentries from (select sl1.switch_ip, sl1.switch_name, count(ar.switch_ip) as arpentries
from my_switchlist sl1
left Join my_arptable ar on ar.switch_ip = sl1.switch_ip
group by sl1.switch_ip,sl1.switch_name
order by sl1.switch_ip))
from my_switchlist sl
left Join my_mactable m on m.switch_ip = sl.switch_ip
group by sl.switch_ip,sl.switch_name
order by sl.switch_ip
The select and the sub-select work fine if they are executed separately.
But as soon as I put them together I get the following error:
Error: A subquery has returned not exactly one row.
SQLState: 21000
ErrorCode: -284
Position: 470
Looks like you want both the 'count' aggregates, which should be possible with something like this:
select
macquery.switch_ip,
macquery.switch_name,
macquery.macentries,
arpquery.arpentries
from
(
select
sl.switch_ip as switch_ip,
sl.switch_name as switch_name,
count(m.switch_ip) as macentries
from my_switchlist sl
left outer join my_mactable m
on m.switch_ip = sl.switch_ip
group by
sl.switch_ip,
sl.switch_name
) macquery
join
(
select
sl1.switch_ip as switch_ip,
sl1.switch_name as switch_name,
count(ar.switch_ip) as arpentries
from my_switchlist sl1
left outer join my_arptable ar
on ar.switch_ip = sl1.switch_ip
group by
sl1.switch_ip,
sl1.switch_name
) arpquery
on (macquery.switch_ip = arpquery.switch_ip
and macquery.switch_name = arpquery.switch_name)
Probably there are more than one "sl1.switch_ip,sl1.switch_name" groups in your "my_switchlist, my_arptable" join.
select arpentries from (select sl1.switch_ip, sl1.switch_name, count(ar.switch_ip) as arpentries
from my_switchlist sl1
left Join my_arptable ar on ar.switch_ip = sl1.switch_ip
group by sl1.switch_ip,sl1.switch_name
order by sl1.switch_ip)
The above query should not return more than one result in order you to use its result in your outer query. So probably there is more than one "sl1.switch_ip,sl1.switch_name" group.