sql update with group by clause - sql

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])

Related

UPDATE statement with JOIN in SQL Server Not Working as Expected

I'm attempting to update the LAST_INSPECTION_FW field for all records in the VEHICLES_FW table with the last JOB_DATE_FW for records with the REASON_CODE_FW = 35. However, what's happening is that once the below code is executed, it's not taking into consideration the WHERE clause. This causes all of the records to update when it should just be updating those with the REASON_CODE_FW = 35.
Is there a way to restructure this code to get it working correctly? Please help, thanks!
UPDATE VEHICLES_FW
SET VEHICLES_FW.LAST_INSPECTION_FW = JOB_HEADERS_FW.FIELD2MAX
FROM VEHICLES_FW
INNER JOIN (SELECT VEHICLE_ID_FW, MAX(JOB_DATE_FW) AS FIELD2MAX
FROM JOB_HEADERS_FW
GROUP BY VEHICLE_ID_FW) AS JOB_HEADERS_FW
ON VEHICLES_FW.VEHICLE_ID_FW = JOB_HEADERS_FW.VEHICLE_ID_FW
INNER JOIN JOB_DETAILS_FW
ON JOB_NUMBER_FW = JOB_NUMBER_FW
WHERE REASON_CODE_FW = '35'
Common Table Expressions are your friend here. SQL Server's strange UPDATE ... FROM syntax is not. EG
with JOB_HEADERS_FW_BY_VEHICLE_ID as
(
SELECT VEHICLE_ID_FW, MAX(JOB_DATE_FW) AS FIELD2MAX
FROM JOB_HEADERS_FW
GROUP BY VEHICLE_ID_FW
), q as
(
Select VEHICLES_FW.LAST_INSPECTION_FW, JOB_HEADERS_FW_BY_VEHICLE_ID.FIELD2MAX NEW_LAST_INSPECTION_FW
FROM VEHICLES_FW
INNER JOIN JOB_HEADERS_FW_BY_VEHICLE_ID
ON VEHICLES_FW.VEHICLE_ID_FW = JOB_HEADERS_FW_BY_VEHICLE_ID.VEHICLE_ID_FW
INNER JOIN JOB_DETAILS_FW
ON JOB_NUMBER_FW = JOB_NUMBER_FW
WHERE REASON_CODE_FW = '35'
)
UPDATE q set LAST_INSPECTION_FW = NEW_LAST_INSPECTION_FW
I suspect this does what you want:
update v
set last_inspection_fw = (
select max(j.job_date_fw)
from job_headers_fw j
inner join job_details_fw jd on jd.job_number_fw = j.job_number_fw
where j.vehicle_id_fw = v.vehicle_id_fw and jd.reason_code_fw = 35
)
from vehicles_fw v

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.

Incorrect Syntax - 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.

SQL error on UPDATE view with GROUP BY

This is the view
SELECT src.OfferAngebotsnummer AS OAngNr1,
SUM(src.Summe2) AS Summe,
CSDokument_1.OfferAngebotsnummer AS OAngNr2,
CSDokument_1.Auftragsvolumen
FROM (
SELECT OfferAngebotsnummer,
ROUND(Angebotssumme, 2) AS Summe2
FROM dbo.CSDokument
WHERE (MANeu = 'AS400') AND
(Art = '3') AND
(DokumentTyp = '3')) AS src
INNER JOIN
dbo.CSDokument AS CSDokument_1 ON
src.OfferAngebotsnummer = CSDokument_1.OfferAngebotsnummer
GROUP BY src.OfferAngebotsnummer,
CSDokument_1.OfferAngebotsnummer,
CSDokument_1.Auftragsvolumen
And this is the UPDATE statement
update UpdateAuftragsvolumenAngebot
set Auftragsvolumen = Summe
where Auftragsvolumen <> Summe
But I get an error that it's not allowed to use UPDATE on view with group by clause.
Cannot update the view or function 'UpdateAuftragsvolumenAngebot'
because it contains aggregates, or a DISTINCT or GROUP BY clause,
or PIVOT or UNPIVOT operator.
How can I accomplish the UPDATE?
I would suggest not using the view and just move it into a correlated sub-query like the below. I suggest that because as soon as you aggregate a view you cannot update the underlying tables.
update CSDokument
set Auftragsvolumen = Summe
from CSDokument
inner join
(
SELECT OfferAngebotsnummer,
ROUND(Angebotssumme, 2) AS Summe2
FROM dbo.CSDokument
WHERE (MANeu = 'AS400') AND
(Art = '3') AND
(DokumentTyp = '3')) AS src
INNER JOIN
dbo.CSDokument AS CSDokument_1 ON
src.OfferAngebotsnummer = CSDokument_1.OfferAngebotsnummer
GROUP BY src.OfferAngebotsnummer,
CSDokument_1.OfferAngebotsnummer,
CSDokument_1.Auftragsvolumen
) as s
on s.OfferAngebotsnummer = CSDokument.OfferAngebotsnummer
where CSDokument.Auftragsvolumen <> s.Summe
I believe you should use the group by in your sub-query and not at the join stage.

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