I have the following query
UPDATE mainList
INNER JOIN custToList D
ON l.custToList_id = d.custToList_id
INNER JOIN dealert dt
ON d.dealert_id = dt.dealert_id
INNER JOIN markList m
on m.markList_id = d.markList_id
SET EXPIRATION_TIMESTAMP = CURRENT_TIMESTAMP
where dt.custNum = 2;
BUt it's telling me the first 'inner' is not valid? Can you not do inner joins on updates in db2?
I don't think DB2 supports JOIN in UPDATE. In your case, this is easily fixed by moving the filtering to the WHERE clause:
UPDATE mainList l
SET EXPIRATION_TIMESTAMP = CURRENT_TIMESTAMP
WHERE EXISTS (SELECT 1
FROM custToList D INNER JOIN
dealert dt
ON d.dealert_id = dt.dealert_id INNER JOIN
markList m
ON m.markList_id = d.markList_id
WHERE l.custToList_id = d.custToList_id AND dt.custNum = 2
);
Related
I am writing an equivalent logic in BQ as in my source system. In source SQL server side it is working fine. But in Big query it is failing with the OR condition in the last left outer join condition. If I am moving the OR condition in the where clause it is giving wrong count. Need help to fix this issue. How can I re write the below query ?
SELECT count(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON (DIM.diSet = PRO.diSet)
INNER JOIN DQConfig CFG
ON (CFG.ConSet = PRO.ConSet)
LEFT OUTER JOIN AgSt CCT
ON (CCT.StSet = PRO.StSet)
INNER JOIN stprof SummPRO
ON (SummPRO.diSet = DIM.SummdiSet AND
SummPRO.dIntervalStart = PRO.dIntervalStart AND
SummPRO.SiteId = PRO.SiteId AND
SummPRO.nDuration = PRO.nDuration)
LEFT OUTER JOIN AgSt SummCCT
ON (SummCCT.StSet = SummPRO.StSet)
LEFT OUTER JOIN AgentStatus SummSTS
ON (
SummSTS.StSet = SummPRO.StSet
OR
SummSTS.StSet = PRO.StSet)
WHERE DIM.cType = 'A'
You can replace LEFT JOIN with CROSS JOIN and move condition from ON clause to WHERE clause as in below example
#standardSQL
SELECT COUNT(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON DIM.diSet = PRO.diSet
INNER JOIN DQConfig CFG
ON CFG.ConSet = PRO.ConSet
LEFT OUTER JOIN AgSt CCT
ON CCT.StSet = PRO.StSet
INNER JOIN stprof SummPRO
ON SummPRO.diSet = DIM.SummdiSet
AND SummPRO.dIntervalStart = PRO.dIntervalStart
AND SummPRO.SiteId = PRO.SiteId
AND SummPRO.nDuration = PRO.nDuration
LEFT OUTER JOIN AgSt SummCCT
ON SummCCT.StSet = SummPRO.StSet
CROSS JOIN AgentStatus SummSTS
WHERE DIM.cType = 'A'
AND (
SummSTS.StSet = SummPRO.StSet
OR SummSTS.StSet = PRO.StSet
)
What is the best join strategy/indexes for this query:
SELECT
kwk.*, an.AuftragDatum, an.AbgabeDatum, an.BezahltDatum, an.AuftragStatus
FROM
KundenWerbenKunden kwk
INNER JOIN
Auftrag an ON an.AuftragNummer = kwk.AuftragNummer
WHERE
kwk.Deleted = 0
Table KundenWerbenKunden has 103950 rows with 103646 Deleted = 0 ones.
Table Auftrag has 3826552 rows.
In my real query I make some more joins:
INNER JOIN
Filiale fn WITH (NOLOCK) ON an.FilialeID = fn.FilialeID
INNER JOIN
Kunde kn ON an.KundeID = kn.KundeID
OUTER APPLY
(SELECT DISTINCT KSKNr
FROM KdZuordnung
WHERE KundeID = kn.KundeID) zn
LEFT JOIN
Anrede ann WITH (NOLOCK) ON kn.Anrede = ann.Anrede
INNER JOIN
AuftragArt aa WITH (NOLOCK) ON an.AuftragArtID = aa.AuftragArtID
INNER JOIN
AuftragGrund ag WITH (NOLOCK) ON an.AuftragGrundID = ag.AuftragGrundID
INNER JOIN
AuftragType at WITH (NOLOCK) ON an.AuftragTypeID = at.AuftragTypeID
For this query:
SELECT *
FROM KundenWerbenKunden kwk INNER JOIN
Auftrag an
ON an.AuftragNummer = kwk.AuftragNummer
WHERE kwk.Geloescht = 0;
And not knowing anything about the distribution of Geloescht, I would first try indexes on KundenWerbenKunden(Geloescht, AuftragNummer) and Auftrag(AuftragNummer).
update dsalesinvoicehdr set paymentterms = ( select pt.termcode from dsalesinvoicehdr dsh
inner join mg_subledger mp on mp.mg_subledgerid = dsh.customer
inner join mg_partytbill mb on mb.bsubledger = mp.subledgercode
left join payterm pt on pt.paytermid = mb.paymentterm)
ORA-01427: single-row subquery returns more than one row
how to resolve this issue. pls help
Will cause this error because your subquery returns multiple pieces of information Your subquery return multi-row data
You can use UPDATE and JOIN
update
(
select pt.termcode,dsh.paymentterms
from dsalesinvoicehdr dsh
inner join mg_subledger mp on mp.mg_subledgerid = dsh.customer
inner join mg_partytbill mb on mb.bsubledger = mp.subledgercode
left join payterm pt on pt.paytermid = mb.paymentterm
) t
set t.termcode= t.paymentterms
Use update and join mate!!
update
(
select pt.termcode,dsh.paymentterms
from dsalesinvoicehdr dsh
inner join mg_subledger mp on mp.mg_subledgerid = dsh.customer
inner join mg_partytbill mb on mb.bsubledger = mp.subledgercode
left join payterm pt on pt.paytermid = mb.paymentterm
) tab
set tab.termcode= tab.paymentterms
You need to select one of the values. To common methods are an aggregation function or where rownum = 1:
update dsalesinvoicehdr
set paymentterms = (select pt.termcode
from dsalesinvoicehdr dsh inner join
mg_subledger mp
on mp.mg_subledgerid = dsh.customer inner join
mg_partytbill mb
on mb.bsubledger = mp.subledgercode join
payterm pt
on pt.paytermid = mb.paymentterm
where rownum = 1
);
You don't need the left join in the subquery. Non-matches will result in NULL anyway.
I have SELECT statement that querying columns from tblQuotes. Why if I am selecting columns a.ProducerCompositeCommission and a.CompanyCompositeCommission, then query spinning forever.
Execution plans with and without those columns are IDENTICAL!
If I commented them out - then it brings result for 1 second.
SELECT
a.stateid risk_state1,
--those columns slows down performance
a.ProducerCompositeCommission,
a.CompanyCompositeCommission,
GETDATE() runDate
FROM
tblQuotes a
INNER JOIN
lstlines l ON a.LineGUID = l.LineGUID
INNER JOIN
tblSubmissionGroup tsg ON tsg.SubmissionGroupGUID = a.SubmissionGroupGuid
INNER JOIN
tblUsers u ON u.UserGuid = tsg.UnderwriterUserGuid
INNER JOIN
tblUsers u2 ON u2.UserGuid = a.UnderwriterUserGuid
LEFT OUTER JOIN
tblFin_Invoices tfi ON tfi.QuoteID = a.QuoteID AND tfi.failed <> 1
INNER JOIN
lstPolicyTypes lpt ON lpt.policytypeid = a.policytypeid
INNER JOIN
tblproducercontacts prodC ON prodC.producercontactguid = a.producercontactguid
INNER JOIN
tblProducerLocations pl ON pl.producerlocationguid = prodc.producerlocationguid
INNER JOIN
tblproducers prod ON prod.ProducerGUID = pl.ProducerGUID
LEFT OUTER JOIN
Catalytic_tbl_Model_Analysis aia ON aia.ImsControl = a.controlno
AND aia.analysisid = (SELECT TOP 1 tma2.analysisid
FROM Catalytic_tbl_Model_Analysis tma2
WHERE tma2.imscontrol = a.controlno)
LEFT OUTER JOIN
Catalytic_tbl_RDR_Analysis rdr ON rdr.ImsControl = a.controlno
AND rdr.analysisid = (SELECT TOP 1 tma2.analysisid
FROM Catalytic_tbl_RDR_Analysis tma2
WHERE tma2.imscontrol = a.controlno)
LEFT OUTER JOIN
tblProducerContacts mnged ON mnged.producercontactguid = ProdC.ManagedBy
LEFT OUTER JOIN
lstQuoteStatusReasons r1 ON r1.id = a.QuoteStatusReasonID
WHERE
l.LineName = 'EARTHQUAKE'
AND CAST(a.EffectiveDate AS DATE) >= CAST('2017-01-01' AS DATE)
AND CAST(a.EffectiveDate AS DATE) <= CAST('2017-12-31' AS DATE)
ORDER BY
a.effectiveDate
The execution plan can be found here:
https://www.brentozar.com/pastetheplan/?id=rJawDkTx-
I ran sp_help and this is what I see:
What exactly wrong with those columns?
I dont use them in a JOIN or anything. Why such bahaviour?
Table Size:
Indexes on table tblQuotes
I need to update an existing table with data from another. My CTE is giving me correct result, but when I'm trying to update with the CTE SSMS complains on
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near '.'.
or Invalid column names at the lines below:
set cm.Action.Identifier_fk = ID
set cm.ActionRequestedAction = Action
set cm.Action.apartment_fk = apartment_fk
This is the code:
Use DB;
GO
with CTE (ID,Action,Identifier_fk,apartment_fk) AS
(select a.ID, a.Action, b.Identifier_fk, m.apartment_fk
from Project.AllSent a (nolock)
left outer join cm.Action b (nolock) on a.ID=b.Identifier_fk
left Outer Join csv.Matching m (nolock) on m.Identifier_fk = a.ID
left outer join csv.Apartment p (nolock) on m.apartment_fk=p.apartment_pk
where b.Identifier_fk is NULL)
update cm.Action
set cm.Action.Identifier_fk = ID
set cm.Action.RequestedAction = Action
set cm.Action.apartment_fk = apartment_fk
from CTE c
JOIN Project.AllSent t (nolock) on t.ID=c.ID;
The correct update statement has only one set. Also, you have no cm in the from clause. I'm going to propose getting rid of the CTE:
update b
set Identifier_fk = a.ID,
Action.RequestedAction = a.Action,
apartment_fk = mm.apartment_fk
from Project.AllSent a (nolock) left join
cm.Action b (nolock)
on a.ID = b.Identifier_fk left join
csv.Matching m (nolock)
on m.Identifier_fk = a.ID left join
csv.Apartment p (nolock)
on m.apartment_fk = p.apartment_pk
where b.Identifier_fk is NULL;
I don't think the final join is necessary.