• Khalid Hanif-458693 (7/16/2013)


    If i upgrade to SQL Server 2012, Can I use the new analytical Functions?

    You could use the new functions in 2012 to do this, but I'm not sure that this would be any cheaper than using a ROW_NUMBER()

    SELECT

    i.Cost_Center_code

    ,i.Payment_code

    ,i.INV_No

    ,LAG(i.Cost_Center_code, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [prev_cost_center_code]

    ,LAG(i.Payment_code, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [prev_payment_code]

    ,LAG(i.INV_No, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [prev_Inv_no]

    ,LEAD(i.Cost_Center_code, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [next_cost_center_code]

    ,LEAD(i.Payment_code, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [next_payment_code]

    ,LEAD(i.INV_No, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [next_Inv_no]

    FROM

    #Invoice_t AS i;