Simplify update-statement - sql

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

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.

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.

update with subquery

The code :
UPDATE tt_t_documents
SET t_Doc_header_ID = (SELECT
MIN(dh.Doc_header_ID)
FROM tt_t_documents td WITH (NOLOCK)
JOIN Doc_header dh WITH (NOLOCK)
ON dh.DH_doc_number = td.t_dh_doc_number
AND dh.DH_sub = 1
JOIN Pred_entry pe WITH (NOLOCK)
ON pe.Pred_entry_ID = dh.DH_pred_entry
JOIN Doc_type dt WITH (NOLOCK)
ON dty.Doc_type_ID = pe.PD_doc_type
AND dt.DT_mode = 5
HAVING COUNT(dh.Doc_header_ID) = 1);
I want to update my columns, but before that I also want to check if there is only one ID found.
The problem in this select is that I get more than one ID.
How can I write a query that updates each row and checks in the same query that there is only one id found?
I am guessing that you intend something like this:
update td
set t_Doc_header_ID = min_Doc_header_ID
from tt_t_documents td join
(select DH_doc_number, min(dh.Doc_header_ID) as min_Doc_header_ID
from Doc_header dh join
Pred_entry pe
on pe.Pred_entry_ID = dh.DH_pred_entry join
Doc_type dt
on dty.Doc_type_ID = pe.PD_doc_type and dt.DT_mode = 5
where dh.DH_doc_number = td.t_dh_doc_number and dh.DH_sub = 1
group by DH_doc_number
having count(dh.Doc_header_ID) = 1
) dh
on dh.DH_doc_number = td.t_dh_doc_number;
Using a join also means that you do not update the values where the condition does not match. If you use a left join, then the values will be updated to NULL (if that is your intention).
I'm not sure I believe you are getting more than one id back with that select given that you are doing a 'min' on it and have no group by. It should be only returning the lowest value for Doc_header_id.
To do what you are asking, first, you should have some way of joining to the tt_t_documents table in the update statement (eg. where td.id == tt_t_documents.id).
Second, you could re-write it to use the sub-query in the from. Something like:
update
tt_t_documents
set
t_Doc_header_ID = x.Doc_Header_id
from tt_t_documents join (
select td.id,
min(dh.Doc_header_ID)
from
tt_t_documents td
join Doc_header dh
on dh.DH_doc_number = td.t_dh_doc_number
and dh.DH_sub = 1
join Pred_entry pe
on pe.Pred_entry_ID = dh.DH_pred_entry
join Doc_type dt
on dty.Doc_type_ID = pe.PD_doc_type
and dt.DT_mode = 5
group by td.id
having
count(dh.Doc_header_ID) = 1
) x on tt_t_documents.id= x.id;
The syntax may not be perfect and I'm not sure how you want to find the doc_header_id but it would be something like this. The sub query would only return values with 1 doc_header_id). Not knowing the schema of your tables, this is as close as I can get.

Subquery returns more than one value

SELECT [Acc_DocumentNo],[Acc_DocumentDate],[Acc_DocumentType],[Acc_DocumentRef],[Acc_DocumentAmount],
[Acc_CustomerID],[Acc_DebtorGroupID],[Acc_DebtorBranchID],
(SELECT Acc_ManualReferenceNo
FROM Acc_CreditDocuments
WHERE Acc_DocumentNo IN
(SELECT Acc_DocumentRef FROM Acc_CreditDocuments
WHERE Acc_DocumentNo = #DocumentNo)) As [Acc_ManualReferenceNo],
[Acc_Status],[Acc_CreatedBy],[Acc_CreatedAt],[Acc_ModifiedBy],[Acc_ModifiedAt]
FROM Acc_CreditDocuments
WHERE Acc_DocumentNo = #DocumentNo AND
Acc_DocumentRef <> Acc_DocumentNo
In above query, subquery statement for selecting Acc_ManualReferenceNo returns more than one value if it has more than one DocumentNo.
Can I open db cursor to fetch and insert the values one by one? Or is there any other way to do it?
Any help will be much appreciated!
JOIN the table with itself instead, like this:
SELECT
a1.[Acc_DocumentNo],
a1.[Acc_DocumentDate],
a1.[Acc_DocumentType],
a1.[Acc_DocumentRef],
a1.[Acc_DocumentAmount],
a1.[Acc_CustomerID],
a1.[Acc_DebtorGroupID],
a1.[Acc_DebtorBranchID],
a2.Acc_ManualReferenceNo As [Acc_ManualReferenceNo] -- < --
...
FROM Acc_CreditDocuments AS a1
INNER JOIN Acc_CreditDocuments AS a2 ON a1.cc_DocumentRef = a2.Acc_DocumentNo
WHERE Acc_DocumentNo = #DocumentNo
AND a1.Acc_DocumentRef <> a1.Acc_DocumentNo