trying to add alias for value generated by Where clause

  • Really? That's a compliment that boosts my confidence. Thanks!

    --Quote me

  • So, like this? Note the index statement.

    select

    RANK() over (order by saledate) as rankOrder

    , saledate

    , saleprice

    into #temp

    from sales;

    select * from #temp;

    create index i_saledate on #temp(saledate);

    ;WITH CTE (rankOrder, SaleDate, [SalePrice], [Running Total])

    AS

    (

    SELECTrankOrder,

    SaleDate,

    [SalePrice],

    [SalePrice]

    FROM #temp

    WHERE rankOrder = 1

    UNION ALL

    SELECTa.rankOrder,

    a.SaleDate,

    a.[SalePrice],

    CTE.[Running Total] + a.[SalePrice]

    FROM CTE

    JOIN #temp a ON CTE.rankOrder + 1 = a.rankOrder

    )

    SELECT * FROM CTE

    OPTION (MAXRECURSION 0)

    GO

    --Quote me

Viewing 2 posts - 16 through 16 (of 16 total)

You must be logged in to reply to this topic. Login to reply