Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Prev and Next Row Without RowNumber Rate Topic Display Mode Topic Options
Author
 Message
 Posted Tuesday, July 16, 2013 7:29 AM
 Grasshopper Group: General Forum Members Last Login: Friday, July 19, 2013 3:25 AM Points: 10, Visits: 48
 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]GOINSERT [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.
Post #1474123
 Posted Tuesday, July 16, 2013 7:35 AM
 SSC-Forever Group: General Forum Members Last Login: Today @ 8:27 AM Points: 40,054, Visits: 32,772
 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 ShawMicrosoft Certified Master: SQL Server 2008, MVPSQL In The Wild: Discussions on DB performance with occasional diversions into recoverabilityWe walk in the dark places no others will enterWe stand on the bridge and no one may pass
Post #1474130
 Posted Tuesday, July 16, 2013 10:53 AM
 Grasshopper Group: General Forum Members Last Login: Friday, July 19, 2013 3:25 AM Points: 10, Visits: 48
 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.
Post #1474228
 Posted Tuesday, July 16, 2013 10:58 AM
 SSC-Forever Group: General Forum Members Last Login: Today @ 8:27 AM Points: 40,054, Visits: 32,772
 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 ShawMicrosoft Certified Master: SQL Server 2008, MVPSQL In The Wild: Discussions on DB performance with occasional diversions into recoverabilityWe walk in the dark places no others will enterWe stand on the bridge and no one may pass
Post #1474232
 Posted Tuesday, July 16, 2013 11:11 AM
 Grasshopper Group: General Forum Members Last Login: Friday, July 19, 2013 3:25 AM Points: 10, Visits: 48
 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.
Post #1474233
 Posted Tuesday, July 16, 2013 11:19 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Today @ 10:06 AM Points: 186, Visits: 1,282
 Khalid Hanif-458693 (7/16/2013)The columns would be prevcostcenter, prevpaymentcode, previnvno, nextcostcenter, nextpaymentcode, nextinvnoI 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 intDECLARE @Current_Payment_code intDECLARE @Current_INV_No intSET @Current_Cost_Center_code = 1SET @Current_Payment_code = 4SET @Current_INV_No = 2SELECT * FROM(SELECT TOP 1 'Previous' WHICH_ROW, Cost_Center_code, Payment_code, INV_NoFROM INVOICE_TWHERE Cost_Center_code < @Current_Cost_Center_codeOR (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) PREVUNIONSELECT * FROM(SELECT TOP 1 'Next' WHICH_ROW, Cost_Center_code, Payment_code, INV_NoFROM INVOICE_TWHERE Cost_Center_code > @Current_Cost_Center_codeOR (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`
Post #1474236
 Posted Tuesday, July 16, 2013 11:19 AM
 SSC-Addicted Group: General Forum Members Last Login: Sunday, September 29, 2013 1:24 AM Points: 429, Visits: 1,721
 ASSUMING the order is dependent on the Payment_Code, then:Sample data:`IF OBJECT_ID('tempdb..#Invoice_t') IS NOT NULLDROP TABLE #Invoice_tCREATE 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]GOINSERT #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 NextCodeFROM #Invoice_t AS t1ORDER BY t1.Payment_code`Output example (if you comment out one or more of the INSERT statementsthe correct Prev/Next values will display leaving out the missing value).`Cost_Center_code INV_No PrevCode Payment_code NextCode1 2 NULL 1 21 2 1 2 31 2 2 3 41 2 3 4 61 2 4 6 71 2 6 7 91 2 7 9 101 2 9 10 NULL`
Post #1474237
 Posted Tuesday, July 16, 2013 11:26 AM
 Grasshopper Group: General Forum Members Last Login: Friday, July 19, 2013 3:25 AM Points: 10, Visits: 48
 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.
Post #1474239
 Posted Tuesday, July 16, 2013 11:52 AM
 Grasshopper Group: General Forum Members Last Login: Tuesday, October 29, 2013 12:47 PM Points: 22, Visits: 144
 Help us understand your requirements:If you picked:cost_center_code: 1Payment_code: 4Inv_no: 2What 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:
Post #1474243
 Posted Tuesday, July 16, 2013 1:41 PM
 Grasshopper Group: General Forum Members Last Login: Friday, July 19, 2013 3:25 AM Points: 10, Visits: 48
 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: 1Payment_code: 4Inv_no: 2What 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:
Post #1474274

 Permissions