Hrm. Seems like your CTE declaration is missing a few pieces; I believe it should be like so:
with UnloadDates(ShipmentId,StartTime,EndTime) as(
select DISTINCT ShipmentID,
(select Min(starttime)
from tblInvoice I
where I.ShipmentID = O.shipmentID
and DataSent is null
) StartTime,
(select Max(Endtime)
from tblInvoice I
where I.ShipmentID = O.shipmentID
and DataSent is null
) EndTime
from tblInvoice O
where O.startTime IS NOT NULL
and O.EndTime IS NOT NULL
and O.DataSent is null)
The CTE needs to have a column listing in the format of: CTEName(Column1,Column2,...). After the AS, you need a starting parenthesis, which you close at the end of your CTE query.
Post back if it's still giving you issues; I believe I caught the errors, but there might be something I overlooked.
EDIT: Doh, forgot the closing parenthesis.
- 😀