• SalvageDog (2/21/2014)


    I too am not a fan of the NextDay column. In this age of the DATE data type, why not convert SaleDate to DATE and equate it to the calendar table date? The query plans are roughly the same, and it makes the join a bit simpler.

    SELECT

    CD.DayDate, CD.NameOfDay

    , COUNT(S.SaleID) AS NumberOfSales

    , SUM(ISNULL(S.SaleAmt, 0)) AS DaySales

    FROM CalDay CD

    LEFT JOIN Sales S ON

    CONVERT(DATE, S.SaleDate) = CD.DayDate

    AND S.SlsRepID IN (0, 3)

    WHERE CD.DayID BETWEEN @FirstDay And @FirstDay + 6

    GROUP BY CD.DayDate, CD.NameOfDay

    ORDER BY CD.DayDate;

    Unless that turns out to somehow be SARGable code (I don't believe it will but haven't tested it), that would be the reason why.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)