At a glance, how do I improve this query to get rid of all the subselects? Or at least as much as i can.

				UPDATE a
				  SET a.VoyageID = @voyageId, -- set to Current Voyage being transferred
				      a.VoyageLegID = (SELECT aa.VoyageLegID  FROM [dbo].[vwVoyageLeg] aa WHERE aa.AssociatedVoyageLegID = a.VoyageLegID) 
				FROM [dbo].[tblVesselMessage] a				
				WHERE a.VoyageID = (SELECT MAX(dd.VoyageID) FROM [dbo].[vwVoyageLeg] cc
							        	JOIN [dbo].[vwVoyageLeg] dd ON dd.VoyageLegID = cc.AssociatedVoyageLegID WHERE	cc.VoyageID = @voyageId) 
 		        	AND a.VoyageLegID = (SELECT ff.VoyageLegID FROM [dbo].[vwVoyageLeg] ee
							 JOIN [dbo].[vwVoyageLeg] ff ON ff.VoyageLegID = ee.AssociatedVoyageLegID WHERE	ee.VoyageID = @voyageId and ff.VoyageLegID = a.VoyageLegID) 
				    AND a.VesselID = @paramVesselID
				    AND a.EVFAcctID = @paramUserCompanyID 
					AND a.IsActive = 1
					AND a.IsDelete = 0   

Open in new window

Paul MaurielloSoftware Programmer Developer Analyst EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
Is this code inside a procedure?
If yes, then you can get rid of this subselect storing the MAX value once before the query, since you dont use any value of the updated table to filter:
a.VoyageID = (SELECT MAX(dd.VoyageID) FROM [dbo].[vwVoyageLeg] cc
JOIN [dbo].[vwVoyageLeg] dd ON dd.VoyageLegID = cc.AssociatedVoyageLegID WHERE	cc.VoyageID = @voyageId) 

Open in new window

Vitor MontalvãoMSSQL Senior EngineerCommented:
You can use CTE to replace the subselects:
WITH CTE_VoyageID (VoyageID)
AS 
	(SELECT MAX(dd.VoyageID) 
	FROM [dbo].[vwVoyageLeg] cc
		INNER JOIN [dbo].[vwVoyageLeg] dd ON dd.VoyageLegID = cc.AssociatedVoyageLegID 
	WHERE cc.VoyageID = @voyageId),

CTE_VoyageLegID (VoyageLegID)
AS 
	(SELECT ff.VoyageLegID 
	FROM [dbo].[vwVoyageLeg] ee
		INNER JOIN [dbo].[vwVoyageLeg] ff ON ff.VoyageLegID = ee.AssociatedVoyageLegID 
	WHERE ee.VoyageID = @voyageId)

UPDATE a
SET a.VoyageID = @voyageId, -- set to Current Voyage being transferred
      a.VoyageLegID = (SELECT aa.VoyageLegID  FROM [dbo].[vwVoyageLeg] aa WHERE aa.AssociatedVoyageLegID = a.VoyageLegID) 
FROM [dbo].[tblVesselMessage] a	
	INNER JOIN CTE_VoyageID m ON a.VoyageID = m.VoyageID
	INNER JOIN CTE_VoyageLegID n ON a.VoyageLegID = n.VoyageLegID
WHERE a.VesselID = @paramVesselID
    AND a.EVFAcctID = @paramUserCompanyID 
	AND a.IsActive = 1
	AND a.IsDelete = 0   

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paul MaurielloSoftware Programmer Developer Analyst EngineerAuthor Commented:
Thank you that was the answer I was looking for
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.