• No ROW_NUMBER() necessary.

    If you know the largest value stored in each of the 3 columns of interest, you could create a COMPUTED PERSISTED column that is INDEXed and do it something like this:

    CREATE TABLE #Invoice_t(

    [Cost_Center_code] [int] NOT NULL,

    [Payment_code] [int] NOT NULL,

    [INV_No] [int] NOT NULL,

    [Composite_key] AS (RIGHT('0000000000'+CAST([Cost_Center_code] AS VARCHAR(9)),10) +

    RIGHT('0000000000'+CAST([Payment_code] AS VARCHAR(9)),10) +

    RIGHT('0000000000'+CAST([INV_No] AS VARCHAR(9)),10)) PERSISTED,

    CONSTRAINT [PK_Invoice_t] PRIMARY KEY CLUSTERED

    (

    [Cost_Center_code] ASC,

    [Payment_code] ASC,

    [INV_No] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    CREATE INDEX composite_index

    ON #Invoice_t ([Composite_key]);

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (1, 1, 1)

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (1, 1, 2)

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (1, 1, 3)

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (2, 1, 1)

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (2, 1, 2)

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (2, 2, 1)

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (2, 2, 2)

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (1, 2, 1)

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (3, 2, 1)

    INSERT #Invoice_t ([Cost_Center_code], [Payment_code], [INV_No]) VALUES (3, 1, 1)

    ---------------------------------------------------------------

    SELECT *

    FROM #Invoice_t;

    SELECT a.Cost_Center_Code, a.Payment_code, a.Inv_NO

    ,Prev.Cost_Center_Code, Prev.Payment_code, Prev.Inv_NO

    ,Next.Cost_Center_Code, Next.Payment_code, Next.Inv_NO

    FROM #Invoice_t a

    OUTER APPLY (

    SELECT TOP 1 Cost_Center_Code, Payment_code, Inv_NO

    FROM #Invoice_t b

    WHERE b.Composite_key < a.Composite_key

    ORDER BY Composite_key DESC) Prev

    OUTER APPLY (

    SELECT TOP 1 Cost_Center_Code, Payment_code, Inv_NO

    FROM #Invoice_t b

    WHERE b.Composite_key > a.Composite_key

    ORDER BY Composite_key ASC) Next

    ORDER BY a.Cost_Center_Code, a.Payment_code, a.Inv_NO;

    GO

    DROP TABLE #Invoice_t;

    Note that the execution plan this produced for the admittedly tiny sample data set showed two INDEX seeks and a CLUSTERED INDEX scan.

    You don't even really need the COMPUTED column, but it does make the final query a bit more terse.

    Edit: Note that originally I overall ordered by Composite_key which generated a NON-CLUSTERED INDEX scan instead of the above stated CLUSTERED INDEX scan.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St