TSQL Update table timing out - sql

The sql in the stored procedure has several updates. I have fixed one and the one left is still timing out.
The table being updated has over 600000 records. The select statement in the sql runs fast when tested alone so it has to be the update part.
UPDATE tblPackage
SET IsUpdated = 1,
ShippingCost = (
SELECT SUM(SC.ShippingCost)
FROM tblShippingCost SC
WHERE SC.PackageID = P.PackageID)
FROM tblShippingCost SC
INNER JOIN tblPackage P ON P.PackageID = SC.PackageID
INNER JOIN tblRunPackage RP ON RP.PackageID = P.PackageID
INNER JOIN tblRun R ON R.RunID = RP.RunID
INNER JOIN tblOrderItem OI ON OI.OrderItemID = R.OrderItemID
INNER JOIN tblOrder O ON O.OrderID = OI.OrderID
WHERE
O.InvoiceID IS NULL
AND SC.PackageID = P.PackageID
I welcome any and all suggestions.
Thanks!

You have joined tblshippingcost an extra time, this will not give the wrong result. But it will take more time joining and calculating. Try removing that part from your update like this:
UPDATE tblPackage
SET
IsUpdated = 1,
ShippingCost = (
SELECT SUM(SC.ShippingCost)
FROM tblShippingCost SC
WHERE SC.PackageID = P.PackageID
)
FROM
tblPackage P
INNER JOIN
tblRunPackage RP ON RP.PackageID = P.PackageID
INNER JOIN
tblRun R ON R.RunID = RP.RunID
INNER JOIN
tblOrderItem OI ON OI.OrderItemID = R.OrderItemID
INNER JOIN
tblOrder O ON O.OrderID = OI.OrderID
WHERE
O.InvoiceID IS NULL

try this
UPDATE tblPackage
SET IsUpdated = 1,
ShippingCost = SUM(SC.ShippingCost)
FROM tblShippingCost SC
INNER JOIN tblPackage P ON P.PackageID = SC.PackageID
INNER JOIN tblRunPackage RP ON RP.PackageID = P.PackageID
INNER JOIN tblRun R ON R.RunID = RP.RunID
INNER JOIN tblOrderItem OI ON OI.OrderItemID = R.OrderItemID
INNER JOIN tblOrder O ON O.OrderID = OI.OrderID
WHERE
O.InvoiceID IS NULL
AND SC.PackageID = P.PackageID

Related

How to get rid of duplicating data in every row?

SELECT distinct AD.ReferenceNumber, AD.ProjectTitle, Z.ZoneCode, C.CompanyName,SS.AssignedTo, ZG.ZoneGroupName,au.Amount
FROM ApplicationDetails AD
LEFT JOIN ApplicationFormsDetails AS b ON (AD.referencenumber = b.referencenumber)
LEFT JOIN ScheduleSummaries AS SS ON (AD.ReferenceNumber = SS.ReferenceNo)
INNER JOIN AppTypes as at on ss.ItemCode = at.Category
INNER JOIN Companies AS C ON (AD.CompanyId = C.CompanyID)
INNER JOIN Zones Z ON (C.ZoneCode = Z.ZoneCode)
INNER JOIN ZoneGroups ZG ON (Z.ZoneGroup = ZG.ZoneGroupId)
LEFT JOIN AssessmentUsedItems au on ah.AssessmentHeaderId = au.HeaderId
WHERE AD.ApplicationDate BETWEEN '2017-10-01' AND '2017-10-31' AND ZG.ZoneGroupCode = 'HO' and ah.referencenumber = 'N-101317-A1-02'
GROUP BY AD.ReferenceNumber, AD.ProjectTitle, Z.ZoneCode, C.CompanyName,SS.AssignedTo, ZG.ZoneGroupName,au.Amount--, ah.ApplicationForm,au.Amount
The output of this query is its duplicating the amount for every AssignTO.
Output :
Maybe you want to try using SUM(ISNULL(au.amount, 0)) AS amount instead of au.amount and remove au.amount from the GROUP BY as well...
Try this query:
SELECT AD.ReferenceNumber,
AD.ProjectTitle,
Z.ZoneCode,
C.CompanyName,
SS.AssignedTo,
ZG.ZoneGroupName,
SUM(COALESCE(au.Amount,0)) AS Amount
FROM ApplicationDetails AD
LEFT JOIN ApplicationFormsDetails AS b
ON (AD.referencenumber = b.referencenumber)
LEFT JOIN ScheduleSummaries AS SS
ON (AD.ReferenceNumber = SS.ReferenceNo)
INNER JOIN AppTypes AS at
ON ss.ItemCode = at.Category
INNER JOIN Companies AS C
ON (AD.CompanyId = C.CompanyID)
INNER JOIN Zones Z
ON (C.ZoneCode = Z.ZoneCode)
INNER JOIN ZoneGroups ZG
ON (Z.ZoneGroup = ZG.ZoneGroupId)
LEFT JOIN AssessmentUsedItems au
ON ah.AssessmentHeaderId = au.HeaderId
WHERE AD.ApplicationDate BETWEEN '2017-10-01' AND '2017-10-31'
AND ZG.ZoneGroupCode = 'HO'
AND ah.referencenumber = 'N-101317-A1-02'
GROUP BY
AD.ReferenceNumber,
AD.ProjectTitle,
Z.ZoneCode,
C.CompanyName,
SS.AssignedTo,
ZG.ZoneGroupName

Long query mistake

I've just written a query here at work but I am getting "ORA-00920: invalid relational operator" when running it and I can't seem to find my mistake.
So here goes:
SELECT
D.DEPARTURE_NO,
D.DEPARTURE_DATE,
I.ITEM_GROUP_ID,
L.LUID,
OA.ADDRESS1,
OA.ADDRESS2,
OA.CITY,
OA.COUNTRY_CODE,
OA.NAME,
OA.POSTAL_CODE,
OA.RECEIVER_ID,
OC.COUNTRY_NAME,
OL.ORDER_LINE_NO,
O.INTERNAL_ORDER_ID,
O.RECEIVER_ORDER_ID,
O.STORER_ID,
O.STORER_ORDER_ID,
PL.BIN_LOCATION_ID,
PLL.PICK_STATUS,
PLL.PICKLIST_LINE_NO,
S.SHIPMENT_ID,
SH.NAME,
SH.SHIPPER_ID,
SS.SHIPPER_SERVICES_ID,
ST.NAME,
SA.ADDRESS_TYPE,
SA.ADDRESS1,
SA.ADDRESS2,
SA.ADDRESS3,
SA.CITY,
SA.POSTAL_CODE,
SC.COUNTRY_NAME,
VOPI.PARENT_INTERNAL_ORDER_ID
FROM
ORDERS O
INNER JOIN VY_ORDER_PARENT_ID VOPI ON VOPI.INTERNAL_ORDER_ID = O.INTERNAL_ORDER_ID
INNER JOIN VY_ORDER_PARENT_ID VOPI ON VOPI.INTERNAL_ORDER_ID = OA.INTERNAL_ORDER_ID
INNER JOIN ORDER_ADDRESS OA ON OA.INTERNAL_ORDER_ID = O.INTERNAL_ORDER_ID
INNER JOIN SHIPMENT S ON S.SHIPMENT_ID = O.SHIPMENT_ID
INNER JOIN SHIPPER_SERVICES SS ON SS.SHIPPER_SERVICES_NO = O.SHIPPER_SERVICES_NO
INNER JOIN STORER ST ON ST.STORER_ID = O.STORER_ID
INNER JOIN ORDER_LINE OL ON OL.INTERNAL_ORDER_ID = O.INTERNAL_ORDER_ID
INNER JOIN SHIPPER SH ON SH.SHIPPER_ID = O.SHIPPER_ID
INNER JOIN ORDER_COUNTRY OC ON OC.COUNTRY_CODE = OA.COUNTRY_CODE
INNER JOIN STORER_COUNTRY SC ON SC.COUNTRY_CODE OA.COUNTRY_CODE
INNER JOIN STORER_ADDRESS SA ON SA.COUNTRY_CODE = SC.STORER_ADDRESS
INNER JOIN STORER ST ON ST.STORER_ID = SA.STORER_ID
INNER JOIN DEPARTURE D ON D.ACTIVE_TRANSPORT_COUNTRY = SC.COUNTRY_CODE
INNER JOIN DEPARTURE D ON D.DEPARTURE_NO = S.DEPARTURE_NO
INNER JOIN ITEM I ON I.COUNTRY_CODE = SC.COUNTRY_CODE
INNER JOIN SHIPPER SH ON SH.SHIPPER_ID = S.SHIPPER_ID
INNER JOIN SHIPPER_SERVICES SS ON SS.SHIPPER_SERVICES_NO = S.SHIPPER_SERVICES_NO
INNER JOIN SHIPPER SH ON SH.SHIPPER_ID = SS.SHIPPER_ID
INNER JOIN IC_TRANSACTION ICT ON ICT.LUID_NO = L.LUID_NO
INNER JOIN PICKLIST_LINE PLL ON PLL.IC_TRANSACTION_NO = ICT.IC_TRANSACTION_NO
INNER JOIN LUID L ON L.STORER_ID = ST.STORER_ID
INNER JOIN ITEM I ON I.STORER_ID = ST.STORER_ID
INNER JOIN ITEM I ON I.ITEM_ID = OL.ITEM_ID AND I.STORER_ID = OL.ITEM_ID
INNER JOIN PICK_LOCATION PL ON PL.ITEM_ID = I.ITEM_ID AND PL.STORER_ID = I.ITEM_ID
INNER JOIN PICKLIST_LINE PLL ON PLL.EXPECTED_BIN_LOCATION_ID = PL.BIN_LOCATION_ID
INNER JOIN PICKLIST_LINE PLL ON PLL.EXPECTED_BUILDING_ID = PL.BUILDING_ID
INNER JOIN ORDER_LINE OL ON OL.INTERNAL_ORDER_ID = PLL.INTERNAL_ORDER_ID AND OL.ORDER_LINE_NO = PLL.ORDER_LINE_NO
WHERE S.SHIPMENT_ID = '2000518517'
Obviously you can't know the exact column names but I've gone through them multiple times and can't find any spelling errors.
Any other errors
INNER JOIN STORER_COUNTRY SC ON SC.COUNTRY_CODE OA.COUNTRY_CODE
should be
INNER JOIN STORER_COUNTRY SC ON SC.COUNTRY_CODE = OA.COUNTRY_CODE
and it looks like
INNER JOIN ORDER_LINE OL ON OL.INTERNAL_ORDER_ID = PLL.INTERNAL_ORDER_ID AND OL.ORDER_LINE NO = PLL.ORDER_LINE_NO
should be ( thanks to LONG )
INNER JOIN ORDER_LINE OL ON OL.INTERNAL_ORDER_ID = PLL.INTERNAL_ORDER_ID AND OL.ORDER_LINE_NO = PLL.ORDER_LINE_NO

Nested query WHERE procedure code IN and AND

Would like to take the following Query and alter it so that it brings back ONLY records where each patient (based on MRN) has BOTH ProcedureCodeList IN ('115-1','117-1','311-1') AND ProcedureCodeList = '119-103'
SELECT P.SiteID, O.ProcedureCodeList, P.MRN, PINFO.LastName, PINFO.FirstName, PINFO.[State] AS Species, PINFO.City AS Breed, O.ProcedureDescList, RF.FieldName, RF.FieldValue, R.ContentText
, R.LastSignDate
FROM ReportFinding RF
INNER JOIN Report R
ON RF.ReportID = R.ReportID
INNER JOIN [Order] O
ON R.ReportID = O.ReportID
INNER JOIN Visit V
ON O.VisitID = V.VisitID
INNER JOIN Patient P
ON P.PatientID = V.PatientID
INNER JOIN PersonalInfo PINFO
ON P.PersonalInfoID = PINFO.PersonalInfoID
WHERE
O.ProcedureCodeList IN ('115-1','117-1','119-103')
ORDER BY R.LastSignDate DESC
There are a couple of ways to solve this. One way is to create two subqueries and join them on the MRN.
SELECT a.SiteID, a.ProcedureCodeList, a.MRN, a.LastName, a.FirstName, a.Species, a.Breed, a.ProcedureDescList, a.FieldName, a.FieldValue, a.ContentText, a.LastSignDate
FROM
(SELECT P.SiteID, O.ProcedureCodeList, P.MRN, PINFO.LastName, PINFO.FirstName, PINFO.[State] AS Species, PINFO.City AS Breed, O.ProcedureDescList, RF.FieldName, RF.FieldValue, R.ContentText, R.LastSignDate
FROM ReportFinding RF
INNER JOIN Report R ON RF.ReportID = R.ReportID
INNER JOIN [Order] O ON R.ReportID = O.ReportID
INNER JOIN Visit V ON O.VisitID = V.VisitID
INNER JOIN Patient P ON P.PatientID = V.PatientID
INNER JOIN PersonalInfo PINFO ON P.PersonalInfoID = PINFO.PersonalInfoID
WHERE O.ProcedureCodeList IN ('115-1','117-1','119-103')) as a
JOIN
(SELECT P.MRN
FROM [Order]
INNER JOIN Visit V ON O.VisitID = V.VisitID
INNER JOIN Patient P ON P.PatientID = V.PatientID
WHERE O.ProcedureCodeList = '119-103') as b ON a.MRN = b.MRN
ORDER BY a.LastSignDate
The PersonalInfo table is not needed in the second query. I don't think ReportFinding and Report are either, based on your JOINs. It depends on what these tables are actually doing.
Another way starts with the original query and adds the following to the WHERE clause (before the ORDER BY):
AND P.MRN IN
(SELECT P.MRN
FROM [Order]
INNER JOIN Visit V ON O.VisitID = V.VisitID
INNER JOIN Patient P ON P.PatientID = V.PatientID
WHERE O.ProcedureCodeList = '119-103')
I would look at the execution plans of both solutions to know which is the better one in this case.

How to select top when already selected fields

Just wanted to ask how to add a 'select top 1 *' when I've already selected fields from a list? I seen examples in other codes but don't quite get it. Thought will be easier if see it in a code I constructed.
Below is an example of a query I have:
select frp.ProductPersonID,frp.FlightSeatId, frp.PlusMealId, per.TitleID, per.surname, per.FirstName, per.PersonTypeId, tor.PersonID, tor.Reference
from package pk
inner join product p on p.packageid = pk.packageid
inner join productperson pp on pp.productid = p.productid
inner join person per on per.personid = pp.personid
left join flightlogicalseat fls on fls.productpersonid = pp.productpersonid
inner join TourOperatorReference tor on tor.PersonID = per.PersonId
inner join FlightReservationPassenger frp on frp.ProductPersonID = pp.ProductPersonId
where pk.Reference LIKE '%'
and ProductTypeId =1
Simply try to use TOP keyword like this:
select TOP 1 frp.ProductPersonID,frp.FlightSeatId, frp.PlusMealId, per.TitleID,
You can just wrap your existing query in new query:
SELECT TOP 1 * FROM
(select frp.ProductPersonID,frp.FlightSeatId, frp.PlusMealId, per.TitleID, per.surname, per.FirstName, per.PersonTypeId, tor.PersonID, tor.Reference
from package pk
inner join product p on p.packageid = pk.packageid
inner join productperson pp on pp.productid = p.productid
inner join person per on per.personid = pp.personid
left join flightlogicalseat fls on fls.productpersonid = pp.productpersonid
inner join TourOperatorReference tor on tor.PersonID = per.PersonId
inner join FlightReservationPassenger frp on frp.ProductPersonID = pp.ProductPersonId
where pk.Reference LIKE '%'
and ProductTypeId =1) t

SQL use nested select in middle of inner join

Is it possible to use a select in the middle of joining...
I am trying to do the following:
FROM
tblorders o
INNER JOIN tblunits u on o.id = u.orderid
INNER JOIN ((SELECT
,Min(n.date) as [MinDate]
from tblNotes n
Where n.test = 'test') te
INNER JOIN tblnotes n on te.id = n.id
and te.[MinDate] = n.AuditinsertTimestamp)
INNER Join tblClient c ON o.ClientId = c.Id
Basically in the select in the middle of the query it is selecting only the notes with min date. The problem is I need to do this here because I need from tblOrders to be the first table.......
Suggestions?
The INNER JOIN failed because you have a leading comma here:
,Min(n.date) as [MinDate]
I think you are looking for something like this:
SELECT ...
FROM tblorders o
INNER JOIN tblunits u on o.id = u.orderid
INNER JOIN (
SELECT id, Min(date) as [MinDate]
from tblNotes
Where test = 'test'
group by id
) te <-- not sure what JOIN clause to use here, please post schema
INNER JOIN tblnotes n on te.id = n.id
and te.[MinDate] = n.AuditinsertTimestamp
INNER Join tblClient c ON o.ClientId = c.Id
You are missing an alias and join condition:
FROM
tblorders o
INNER JOIN tblunits u on o.id = u.orderid
INNER JOIN ((SELECT Min(n.date) as [MinDate]
from tblNotes n
Where n.test = 'test') te
INNER JOIN tblnotes n on te.id = n.id
and te.[MinDate] = n.AuditinsertTimestamp)
-- missing
AS z
ON <join conditions haere>
INNER Join tblClient c ON o.ClientId = c.Id
Yes, you can have a Select in a Join.