SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Prev and Next Row Without RowNumber


Prev and Next Row Without RowNumber

Author
Message
Khalid Hanif-458693
Khalid Hanif-458693
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 50
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.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90803 Visits: 45284
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


Khalid Hanif-458693
Khalid Hanif-458693
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 50
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.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90803 Visits: 45284
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


Khalid Hanif-458693
Khalid Hanif-458693
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 50
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.
patrickmcginnis59 10839
patrickmcginnis59 10839
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1842 Visits: 5527
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



to properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)SSC Eights! (861 reputation)

Group: General Forum Members
Points: 861 Visits: 1721
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_code INV_No PrevCode Payment_code NextCode
1 2 NULL 1 2
1 2 1 2 3
1 2 2 3 4
1 2 3 4 6
1 2 4 6 7
1 2 6 7 9
1 2 7 9 10
1 2 9 10 NULL



 
Khalid Hanif-458693
Khalid Hanif-458693
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 50
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.
keebler96
keebler96
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 147
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:
Khalid Hanif-458693
Khalid Hanif-458693
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 50
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:

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search