Incorrect Syntax - SQL - sql

I am trying to update two fields in my table tbl_prev_data using subquery because I had to use the aggregate SUM. But I don't know why it says
Incorrect Syntax near 't'`
where t is a temp variable that holds the results of the subquery. Am I missing something ? Below is my query.
UPDATE tbl_prev_data
SET Original_Value = t.Original_Value
SET Pre_Val = t.PreWDV
FROM tbl_prev_data
INNER JOIN
(SELECT
Asset_Group_Code,
ISNULL(SUM(Original_Value),0) AS 'Original_Value',
ISNULL(SUM(Prev_Val),0) AS 'PreWDV'
FROM
tbl_Asset_Master
GROUP BY
Asset_Group_Code, DOP
HAVING
(YEAR(DOP) != YEAR(GETDATE()) AND MONTH (DOP) > 3) t ON tbl_prev_data.Asset_Group_Code = t.Asset_Group_Code

You have to specify SET only once:
UPDATE tbl_prev_data
SET Original_Value = t.Original_Value,
Pre_Val = t.PreWDV
FROM tbl_prev_data INNER JOIN ...
Other than that you also need to remove the redundant left parenthesis from the HAVING clause:
HAVING (YEAR(DOP) != YEAR(GETDATE()) AND MONTH (DOP) > 3
-------^

it seems a problem with closing bracket.
(SELECT Asset_Group_Code, ...
is not closed with ) before t.

Related

Incorrect syntax near the keyword 'FROM' in SQL Server

Please help me in the code, I get an error
Incorrect syntax near the keyword 'FROM'
SELECT
produkt.Twr_Kod as kod,
(SELECT
ISNULL(SUM(zasoby.TwZ_Ilosc), 0) + ISNULL(SUM(trs_ilosc), 0)
FROM
cdn.TraSElem
WHERE
trs_typ = 3 AND TrS_TrEIdWydania = 0
AND TrS_DataOpe > GETDATE() AND zasoby.TwZ_TwrId = TrS_TwrId
AND zasoby.TwZ_MagId = 1
FROM
CDN.TwrZasoby as zasoby
WHERE
zasoby.Twz_TwrId = product.Twr_twrid) AS zasoby,
CONVERT(NUMERIC(10, 0), produkt.Twr_IloscMin) AS ilosc_minimalna
FROM
CDN.Towary AS product
LEFT JOIN
CDN.Towary AS produkt ON product.Twr_TwrId = produkt.Twr_TwrId
GROUP BY
product.Twr_TwrId, produkt.Twr_Kod, produkt.Twr_IloscMin
ORDER BY
kod
SQLs kinda need to look like this:
SELECT columns FROM tables_or_queries WHERE predicates GROUP BY grouping_keys ORDER BY columns
This pattern can be nested both inside the SELECT region and the FROM region:
SELECT
column1,
(SELECT columnA FROM table WHERE where_predicates) as column2,
column3
FROM
table
INNER JOIN
(SELECT columnA, columnB FROM table WHERE where_predicates) as nested_query
ON join_predicates
WHERE
where_predicates
But you can't have multiple FROM regions etc.. To get more targeted advice youre going to have to tell us what you're hoping your query does.. Right now, we can only say to sort out the basic syntax errors the compilation process is complaining about
You seem to have multiple FROM statements in a single SELECT statement. Every SELECT can have only one FROM clause. Glancing through your query, I think it's going to give you performance nightmares. Use joins instead of using inline queries. Will improve performance.
SELECT
produkt.Twr_Kod as kod,
(SELECT
ISNULL(SUM(zasoby.TwZ_Ilosc), 0) + ISNULL(SUM(trs_ilosc), 0)
**FROM
cdn.TraSElem
WHERE
trs_typ = 3 AND TrS_TrEIdWydania = 0
AND TrS_DataOpe > GETDATE() AND zasoby.TwZ_TwrId = TrS_TwrId
AND zasoby.TwZ_MagId = 1
FROM
CDN.TwrZasoby as zasoby**
WHERE
zasoby.Twz_TwrId = product.Twr_twrid) AS zasoby,
CONVERT(NUMERIC(10, 0), produkt.Twr_IloscMin) AS ilosc_minimalna
FROM
CDN.Towary AS product
LEFT JOIN
CDN.Towary AS produkt ON product.Twr_TwrId = produkt.Twr_TwrId
GROUP BY
product.Twr_TwrId, produkt.Twr_Kod, produkt.Twr_IloscMin
ORDER BY
kod

How come this query result in missing from clause?

This clause keeps showing "ERROR: missing FROM-clause entry for table "subid" in postgresql 10.
How so?
UPDATE "io_S1"."tc_history"
SET "c_TIME" = TT."c_TIME",
"b_TIME_c"=TT."b_TIME_c",
"b_TIME_COLLECTION"=TT."b_TIME_COLLECTION",
"c_NOTE"=TT."c_NOTE",
"b_TIME_TAXI"=TT."b_TIME_TAXI",
"b_LOCATION_TAXI"=TT."b_LOCATION_TAXI",
"ESTABLISHED_TIME"=TT."c_TIME"
FROM (
SELECT "c_TIME","b_TIME_c","b_TIME_COLLECTION","c_NOTE","b_TIME_TAXI","b_LOCATION_TAXI","c_TIME"
FROM "io_TRACE"."PERSONAL_Tc_RECORD" TR
WHERE TR."PERSONAL_SERIAL_ID" IN (SELECT "SUBJECT_ID"
FROM "io_COLLECTION"."COLLECTION_CONSENT_RECORD" AS SUBID
WHERE SUBID."SUBJECT_CITIZEN_ID" IN (SELECT "SUBJECT_CITIZEN_ID"
FROM "io_S1"."tc_history" AS TC))
) AS TT
WHERE "SUBJECT_CITIZEN_ID"=SUBID."SUBJECT_CITIZEN_ID";
You can try below
UPDATE "io_S1"."tc_history"
SET "c_TIME" = TT."c_TIME",
"b_TIME_c"=TT."b_TIME_c",
"b_TIME_COLLECTION"=TT."b_TIME_COLLECTION",
"c_NOTE"=TT."c_NOTE",
"b_TIME_TAXI"=TT."b_TIME_TAXI",
"b_LOCATION_TAXI"=TT."b_LOCATION_TAXI",
"ESTABLISHED_TIME"=TT."c_TIME"
FROM (
SELECT "c_TIME","b_TIME_c","b_TIME_COLLECTION","c_NOTE","b_TIME_TAXI","b_LOCATION_TAXI","c_TIME",
SUBID."SUBJECT_CITIZEN_ID"
FROM "io_TRACE"."PERSONAL_Tc_RECORD" TR inner join
"io_COLLECTION"."COLLECTION_CONSENT_RECORD" AS SUBID
on TR."PERSONAL_SERIAL_ID"=SUBID."SUBJECT_ID"
inner join "io_S1"."tc_history" AS TC
on TC."SUBJECT_CITIZEN_ID"=SUBID."SUBJECT_CITIZEN_ID"
) AS TT
WHERE "SUBJECT_CITIZEN_ID"=TT."SUBJECT_CITIZEN_ID";
Alias used 'SUBID' is out of scope, therefore, the from clause doesn't recognize it. Best to use the normal convention as 'TableName.ColumnName', since two tables have the same column name.

SQL multipart identifier into GrOUP BY clause

I have a left join like this:
LEFT MERGE JOIN --IVA0A
( SELECT VOUCHER,DATAAREAID,ISNULL(VENDTRANSID,0)AS LJT, SUM(IIF(TAXITEMGROUP = 'ANTICIPOS',-1*TAXBASEAMOUNT,TAXBASEAMOUNT))AS TAXBASEAMOUNT,
SUM(IIF(TAXITEMGROUP = 'ANTICIPOS',-1*TAXAMOUNT,TAXAMOUNT)) AS TAXAMOUNT FROM
##CPP TT
WHERE (
(TT.TAXCODE LIKE 'IVA0A' OR TT.TAXCODE LIKE 'IVA0AFA' OR TT.TAXCODE LIKE 'IVA0AEXP') OR
(TT.TAXITEMGROUP = 'ANTICIPOS' AND (TT.TAXCODE LIKE 'IVA0AP' OR TT.TAXCODE LIKE 'IVA0AFAP' OR TT.TAXCODE LIKE 'IVA0AEXPP'))
)
GROUP BY TT.VOUCHER,TT.DATAAREAID,ISNULL(LJT.VENDTRANSID,0))
AS TTIVA0A ON VT.VOUCHER = TTIVA0A.VOUCHER AND TTIVA0A.DATAAREAID = VT.DATAAREAID
and IIF(TTIVA0A.LJT=0,VT.RECID,TTIVA0A.LJT) = VT.RECID
Problem is when I trying to use GROUP BY clause
SQL returns:
The multi-part identifier "LJT.VENDTRANSID" could not be bound.
I can't understand why it happen, can anyone explain me please? Regards
I suppose that you want to group by ISNULL(VENDTRANSID,0) AS LJT, since you can't use the alias LJT in the WHERE clause you should group using the expression itself:
GROUP BY TT.VOUCHER, TT.DATAAREAID, ISNULL(VENDTRANSID,0)
LJT is the alias given to column ISNULL(VENTTRANSID, 0).
I think that, in GROUP BY clause instead of ISNULL(LJT.VENTTRANSID, 0) if you just give ISNULL(VENTTRANSID, 0) it will solve the issue.

sql update with group by clause

I am trying to update all instances of [xEndAtach] in tblDoc with [EndDoc] from tbldoc, i am pivoting on tbldoc.[begdoc#] = tblWR001.[begdoc#] and grouping by tbldoc.[xBegAttach]
update tbldoc set td.xEndAttach = max(td.[EndDoc#])
from tblWR001 tR join tbldoc tD on td.[begdoc#] = tr.[begDoc#]
group by td.[xBegAttach]
I am getting an error trying to use the [group by] clause... not sure how to work arround that.
thanks for your help in advance guys/gals!
Maybe this... it has to first select the max for the correlated table being updated then it can assign the value.
update tbldoc set td.xEndAttach = (SELECT max(td.[EndDoc#])
from tblWR001 tR join tbldoc tD on td.[begdoc#] = tr.[begDoc#]
group by td.[xBegAttach])

Simplify update-statement

I have the fallowing update-statement:
update tmp set
tmp.Anzahl=(select sum(a.BNANZ) from PAYMASTER_Journal a where a.BNARTID=tmp.ArtikelAutoID),
tmp.Betrag=(select sum(a.BNBETR) from PAYMASTER_Journal a where a.BNARTID=tmp.ArtikelAutoID),
tmp.Rabatt=(select sum(a.BNRMRBETR) from PAYMASTER_Journal a where a.BNARTID=tmp.ArtikelAutoID)
from ##tmp1 tmp
On this way, for each record in ##tmp1, there are 3 subqueries executed. ##tmp1 contains 10'000 records -> totaly 30'000 subqueries.
Because each subquery selects the same records from PAYMASTER_Journal, I am searching for a way, to update ##tmp1 with executing only one subquery per record in ##tmp1.
I hope, someone can help me about this.
Using LEFT JOIN try this
update tmp set
tmp.Anzahl=BNANZ_accumulated,
tmp.Betrag=BNBETR_accumulated,
tmp.Rabatt=BNRMRBETR_accumulated
from ##tmp1 tmp
LEFT JOIN ( SELECT BNARTID,
SUM(BNANZ) AS BNANZ_accumulated,
SUM(BNBETR) AS BNBETR_accumulated,
SUM(BNRMRBETR) AS BNRMRBETR_accumulated
FROM PAYMASTER_Journal
WHERE (ARTIKELAUSWAHL=0x30 AND BLBONSTO=0x30 AND BLZESTO=0x30 AND
STORNO=0x30 AND
BDDAT BETWEEN '20120301' AND '20130821' AND
AdressID='d68e4d8f-e60e-4482-9730-76076948df43' AND
BNFIL=5 AND
ISNULL(Preisliste, 'VK-Preisliste') = 'VK-Preisliste' AND
BNARTID=tmp.ArtikelAutoID)
GROUP BY BNARTID) a ON a.BNARTID=tmp.ArtikelAutoID
this will leave you NULL when there is no rows in PAYMASTER_Journal for a given ##tmp1.ArtikelAutoID
if you don't want to touch them, change the LEFT JOIN to INNER JOIN
I'd go with MERGE statement:
merge ##tmp1
using (select
BNARTID
,sum(BNANZ) as BNANZ
,sum(BNBETR) as BNBETR
,sum(BNRMRBETR) as BNRMRBETR
from
PAYMASTER_Journal
group by
BNARTID
) as upd
on tmp.ArtikelAutoID = upd.BNARTID
when matched then update set Anzahl=BNANZ, Betrag=BNBETR, Rabatt=BNRMRBETR;
You can do it with a single sub-query as follows.
UPDATE tmp T
INNER JOIN
(SELECT a.BNARTID
,sum(a.BNANZ) as BANAZ_SUM
,sum(a.BNBETR) as BNBETR_SUM
,sum(a.BNRMRBETR) as BNRMRBETR_SUM
FROM PAYMASTER_Journal a
WHERE a.BNARTID = tmp.ArtikelAutoID
GROUP BY
a.BNARTID
) SQ
ON SQ.BNARTID = tmp.ArtikelAutoID
SET tmp.Anzahl = SQ.BANAZ_SUM
,tmp.Betrag = SQ.BNBETR_SUM
,tmp.Rabatt = SQ.BNRMRBETR_SUM