Prev and Next Row Without RowNumber

  • Hello All,

    Can anyone help me with this one.

    here is the table sample.

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

    CREATE TABLE [dbo].[Invoice_t](

    [Cost_Center_code] [int] NOT NULL,

    [Payment_code] [int] NOT NULL,

    [INV_No] [int] NOT NULL,

    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

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

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

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

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

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

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

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

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

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

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

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

    How can i get the previous and next row from this type of data, without using rownumbers.

    Thanks.

  • Define 'previous' and 'next'? Tables have no order, so what column is the one that defines which row is the next row?

    And why the restriction against the row number function?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I need all the columns in prev and next. The reason I want to avoid rownumbers because it scans the complete table and is slower if the data is large.

  • Firstly, you didn't answer my question. Previous and next as defined by what column. What column tells me that a row is the next one?

    Row number does not force table scans.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • The columns would be

    prevcostcenter, prevpaymentcode, previnvno, nextcostcenter, nextpaymentcode, nextinvno.

    all the columns define the next or previous row.

    I have tried the rownumber the estimated rows in the execution plan is always the total rows Iin the table. If there are 100,000 rows it take 1-2 seconds to get the data.

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


    The columns would be

    prevcostcenter, prevpaymentcode, previnvno, nextcostcenter, nextpaymentcode, nextinvno

    I have tried the rownumber the estimated rows in the execution plan is always the total rows Iin the table. If there are 100,000 rows it take 1-2 seconds to get the data.

    Well this is one rather wordy approach, wonder how it would do??? Pretty much a straight compare.

    DECLARE @Current_Cost_Center_code int

    DECLARE @Current_Payment_code int

    DECLARE @Current_INV_No int

    SET @Current_Cost_Center_code = 1

    SET @Current_Payment_code = 4

    SET @Current_INV_No = 2

    SELECT * FROM

    (

    SELECT TOP 1 'Previous' WHICH_ROW,

    Cost_Center_code,

    Payment_code,

    INV_No

    FROM INVOICE_T

    WHERE Cost_Center_code < @Current_Cost_Center_code

    OR (Cost_Center_Code = @Current_Cost_Center_code AND Payment_code < @Current_Payment_code)

    OR (Cost_Center_Code = @Current_Cost_Center_code AND Payment_code = @Current_Payment_code AND INV_No < @Current_INV_No)

    ORDER BY Cost_Center_code DESC,

    Payment_code DESC,

    INV_No DESC

    ) PREV

    UNION

    SELECT * FROM

    (

    SELECT TOP 1 'Next' WHICH_ROW,

    Cost_Center_code,

    Payment_code,

    INV_No

    FROM INVOICE_T

    WHERE Cost_Center_code > @Current_Cost_Center_code

    OR (Cost_Center_Code = @Current_Cost_Center_code AND Payment_code > @Current_Payment_code)

    OR (Cost_Center_Code = @Current_Cost_Center_code AND Payment_code = @Current_Payment_code AND INV_No > @Current_INV_No)

    ORDER BY Cost_Center_code ASC,

    Payment_code ASC,

    INV_No ASC

    ) NEXT

  • ASSUMING the order is dependent on the Payment_Code, then:

    Sample data:

    IF OBJECT_ID('tempdb..#Invoice_t') IS NOT NULL

    DROP TABLE #Invoice_t

    CREATE TABLE #Invoice_t(

    [Cost_Center_code] [int] NOT NULL,

    [Payment_code] [int] NOT NULL,

    [INV_No] [int] NOT NULL,

    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

    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, 2, 2)

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

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

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

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

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

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

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

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

    Then:

    SELECT

    t1.Cost_Center_code

    ,t1.INV_No

    ,(SELECT TOP(1) Payment_code

    FROM #Invoice_t

    WHERE Payment_code < t1.Payment_code

    ORDER BY Payment_code DESC)

    AS PrevCode

    ,t1.Payment_code

    ,(SELECT TOP(1) Payment_code

    FROM #Invoice_t

    WHERE Payment_code > t1.Payment_code

    ORDER BY Payment_code ASC)

    AS NextCode

    FROM

    #Invoice_t AS t1

    ORDER BY

    t1.Payment_code

    Output example (if you comment out one or more of the INSERT statements

    the correct Prev/Next values will display leaving out the missing value).

    Cost_Center_codeINV_NoPrevCodePayment_codeNextCode

    12NULL12

    12123

    12234

    12346

    12467

    12679

    127910

    12910NULL

     

  • All the three columns are primary keys. If I select one row by costcenter paymentcode and invno. I would like to know the previous and next costcenter psymentcode and invno.

  • Help us understand your requirements:

    If you picked:

    cost_center_code: 1

    Payment_code: 4

    Inv_no: 2

    What do you expect to see in the following columns?

    prev_cost_center_code:

    prev_Payment_code:

    prev_Inv_no:

    next_cost_center_code:

    next_Payment_code:

    next_Inv_no:

  • Yes, Keebler96, this is exactly how i need the row.

    I have updated the OP with better data.

    The order of the previous and the next rows should be ordered just like the SQL has inserted according to the primary keys.

    keebler96 (7/16/2013)


    Help us understand your requirements:

    If you picked:

    cost_center_code: 1

    Payment_code: 4

    Inv_no: 2

    What do you expect to see in the following columns?

    prev_cost_center_code:

    prev_Payment_code:

    prev_Inv_no:

    next_cost_center_code:

    next_Payment_code:

    next_Inv_no:

  • The order of the previous and the next rows should be ordered just like the SQL has inserted according to the primary keys.

    This is the problem. There is no default order by in SQL Server. You MUST order your data by the use of the order by clause. You must define the proper order for your result set.

    Just because you inserted records in a particular order does not mean that the table stores them in that order.

    From what you have provided us, you have to use Row_Number, and you have to order your composite primary key in the way you want the rows to be ordered.

    Hope that helps.

  • The rownumber is very costly on a large table,

    The Order By can be By Cost_Center_Code, Payment_Code, Inv_No

    Thanks

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

  • keebler96 (7/16/2013)


    The order of the previous and the next rows should be ordered just like the SQL has inserted according to the primary keys.

    This is the problem. There is no default order by in SQL Server. You MUST order your data by the use of the order by clause. You must define the proper order for your result set.

    Just because you inserted records in a particular order does not mean that the table stores them in that order.

    From what you have provided us, you have to use Row_Number, and you have to order your composite primary key in the way you want the rows to be ordered.

    Hope that helps.

    I was able to get the previous and next rows with the SQL I posted and not use row number. I suspect the row number version would be more concise because I had to build up the comparison criteria to account for all keys.

  • patrickmcginnis59 10839 (7/16/2013)


    keebler96 (7/16/2013)


    The order of the previous and the next rows should be ordered just like the SQL has inserted according to the primary keys.

    This is the problem. There is no default order by in SQL Server. You MUST order your data by the use of the order by clause. You must define the proper order for your result set.

    Just because you inserted records in a particular order does not mean that the table stores them in that order.

    From what you have provided us, you have to use Row_Number, and you have to order your composite primary key in the way you want the rows to be ordered.

    Hope that helps.

    I was able to get the previous and next rows with the SQL I posted and not use row number. I suspect the row number version would be more concise because I had to build up the comparison criteria to account for all keys.

    I was not able to get the Previous or the next row, the where condition should include all the columns.

    Cost_Center_Code,Payment_Code,Inv_No.

    Can you check your code with the updated OP code.

Viewing 15 posts - 1 through 15 (of 24 total)

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