Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I have a query which to me looks messey, I'm just wondering if someone can simplify it for me as there has to be a neater way to return the data.
SQL
SELECT DestinationName AS [Destination], cp.RoutingRank, cp.LCRRank AS [LCR Rank], cp.LCRRate AS [Carrer Rate], cp1.LCRRate AS [LCR Rate], cp2.LCRRate AS [Routing Rate],
c1.CarrierShortName AS [LCR Carrier], c2.CarrierShortName AS [1st Carrier], cp1.CarrierID AS [LCRCarrierId], 
ISNULL(ovr.CarrierId, ex.CarrierId) AS [Override Carrier], ISNULL(ovr.ExclusionTypeId, ex.ExclusionTypeId) AS [Exclusion Type]
FROM dbo.Destination d WITH(NOLOCK)
LEFT OUTER JOIN dbo.CarrierPosition cp WITH(NOLOCK) ON d.DestinationId = cp.DestinationId


INNER JOIN 

--Sub Query 1
(SELECT RoutingPlanScheduleId, AnalysisDate, Max(RoutingPlanId) AS RoutingPlanId 
FROM dbo.RoutingPlan WITH(NOLOCK) GROUP BY RoutingPlanScheduleId, AnalysisDate) rp
ON rp.RoutingPlanId = cp.RoutingPlanId


INNER JOIN dbo.RoutingPlanSchedule rps WITH(NOLOCK) ON rps.RoutingPlanScheduleId = rp.RoutingPlanScheduleId
INNER JOIN dbo.CarrierPosition cp1 WITH(NOLOCK) ON cp1.DestinationId = cp.DestinationId  AND cp1.RoutingPlanId = cp.RoutingPlanId AND cp1.LCRRank = 1
INNER JOIN dbo.Carrier c1 WITH(NOLOCK) ON c1.CarrierId = cp1.CarrierId
INNER JOIN dbo.CarrierPosition cp2 WITH(NOLOCK) ON cp2.DestinationId = cp.DestinationId  AND cp2.RoutingPlanId = cp.RoutingPlanId AND cp2.RoutingRank = 1
INNER JOIN dbo.Carrier c2 WITH(NOLOCK) ON c2.CarrierId = cp2.CarrierId
LEFT OUTER JOIN 


-- Sub Query 2
(SELECT ISNULL(e.CarrierId,p.CarrierId) AS CarrierId, ExclusionTypeId, DestinationBandId
FROM dbo.ExclusionDestinationBand edb WITH(NOLOCK)
INNER JOIN dbo.Exclusion e WITH(NOLOCK) ON e.ExclusionId = edb.ExclusionId
INNER JOIN dbo.ExclusionDetail ed WITH(NOLOCK) On ed.ExclusionId = e.ExclusionId 
LEFT OUTER JOIN dbo.ROProportion p WITH(NOLOCK) ON p.ExclusionDetailId = ed.ExclusionDetailId
WHERE ed.Deleted=0 AND GetDate() BETWEEN ed.ActiveFrom AND ed.ActiveTo
AND e.ExclusionTypeId IN (3,4,5,6)) ovr
ON ovr.DestinationBandId = d.DestinationBandId

-- Sub Query 3
LEFT OUTER JOIN 
(SELECT CarrierId, ExclusionTypeId, DestinationBandId
FROM dbo.Exclusion e2 WITH(NOLOCK)
INNER JOIN dbo.ExclusionDetail ed2 WITH(NOLOCK) On ed2.ExclusionId = e2.ExclusionId 
LEFT OUTER JOIN dbo.ExclusionDestinationBand edb2 WITH(NOLOCK) ON edb2.ExclusionId = e2.ExclusionId
WHERE ed2.Deleted=0 AND GetDate() BETWEEN ed2.ActiveFrom AND ed2.ActiveTo
AND e2.ExclusionTypeId NOT IN (3,4,5,6)) ex 
ON ex.CarrierId  = cp1.CarrierId AND (ex.ExclusionTypeId = 1 OR ex.DestinationBandId = d.DestinationBandId)

--Where clause for initial query
WHERE cp.CarrierId = @CarrierID
AND rps.ROsettingId = @ROSetting
--AND rps.CommercialSwitch = 0
AND rp.AnalysisDate = @AnalysisDate
ORDER BY DestinationName


Thanks in advance guys
Posted
Comments
Sunasara Imdadhusen 25-Jun-13 7:06am    
Please provide more details about...what types of your problem, what you would like, what query or code does etc...
frostcox 25-Jun-13 7:19am    
Its not a problem as such, the query returns the data as expected, I just want guidence as to how code it better. Like should I use cte's for the sub queries or what is best practice.

1 solution

We can't do that for you because of lot of reasons...

I would suggest you to read these articles and to follow the steps:
Query optimization[^]
SQL Tuning or SQL Optimization[^]
The Seven Sins against TSQL Performance[^] - my favorite ;)
How To: Optimize SQL Queries[^]
Sql tutorial - improving query performance[^]

Is it enough for start?
 
Share this answer
 
Comments
db_developer 27-Jun-13 5:01am    
thanks for reference about optimization

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900