Home Forums SQL Server 2012 SQL Server 2012 - T-SQL What is wrong with this syntax? Query will work, but CTE will not "compile" RE: What is wrong with this syntax? Query will work, but CTE will not "compile"

  • Since you're correcting this code, why don't we make it simpler and faster?

    WITH UnloadDates AS(

    SELECT ShipmentID,

    MIN(starttime) StartTime,

    MAX(Endtime) EndTime

    FROM tblInvoice O

    WHERE O.startTime IS NOT NULL

    AND O.EndTime IS NOT NULL

    AND O.DataSent is null

    GROUP BY ShipmentID

    )

    SELECT ShipmentID,

    StartTime,

    EndTime

    FROM UnloadDates

    This way you read the table just once instead of trice. It's also a lot less code. 🙂

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2