• scottichrosaviakosmos (9/26/2012)


    I want to make a view from the query . will this CTE works for view.?

    create view DidYouTryIt as

    WITH CTE AS(

    selectcalcid,

    salesamount,

    productid,

    ROW_NUMBER() OVER( PARTITION BY productid ORDER BY calcid) rn

    from abc t1

    --where productid IN (58, 62, 65)

    )

    SELECTcte1.productid,

    cte1.calcid,

    cte1.salesamount,

    cte2.calcid AS prevcalcid,

    cte2.salesamount AS prevsalesamount

    FROM CTE cte1

    LEFT

    JOIN CTE cte2 ON cte1.productid = cte2.productid

    AND cte1.rn = cte2.rn + 1

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/