How to I join a CTE to a main query?
(ps: Thank you again - this community is brilliant)
With tempCTE1locws ([1stchargeableweights], movementref, lastdate)
as
(Select
SUM(c.[Chargeable Weight]) AS [1stchargeableweights],
mm.MovementRef [movementref],
MAX (ts.systemstamp) [lastdate]
FROM dbo.whsConsignment wc
INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference
INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference
inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference]
inner join dbo.movMovement mm on mm.MovementID = m.MovementID
INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID
AND wc.whsHeaderID = wh.WhsHeaderID
AND wc.StatusCode = 'NL'
AND c.[Service Type] = 'F'
AND ts.SlackNoSlack = 'slack'
or ts.slacknoslack like 'slack - tripped%'
WHERE wh.ArrDepDate BETWEEN @StartDate AND @EndDate
GROUP by mm.MovementRef),
I would like to join this CTE to dbo.movmovement on movementref. However, writing it like
From dbo.movmovement m
Inner join dbo.tempCTE1locws locws on locws.movementref = m.movementref
doesn't work at all.