Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Prev and Next Row Without RowNumber Expand / Collapse
Author
Message
Posted Tuesday, July 16, 2013 7:29 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, December 11, 2013 10:09 AM
Points: 10, 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.
Post #1474123
Posted Tuesday, July 16, 2013 7:35 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:09 PM
Points: 42,847, Visits: 35,976
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 2008, MVP
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

Post #1474130
Posted Tuesday, July 16, 2013 10:53 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, December 11, 2013 10:09 AM
Points: 10, 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.
Post #1474228
Posted Tuesday, July 16, 2013 10:58 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:09 PM
Points: 42,847, Visits: 35,976
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 2008, MVP
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

Post #1474232
Posted Tuesday, July 16, 2013 11:11 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, December 11, 2013 10:09 AM
Points: 10, 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.
Post #1474233
Posted Tuesday, July 16, 2013 11:19 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 2:04 PM
Points: 411, Visits: 2,413
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

Post #1474236
Posted Tuesday, July 16, 2013 11:19 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-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 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


 
Post #1474237
Posted Tuesday, July 16, 2013 11:26 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, December 11, 2013 10:09 AM
Points: 10, 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.
Post #1474239
Posted Tuesday, July 16, 2013 11:52 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 14, 2014 1:35 PM
Points: 22, 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:
Post #1474243
Posted Tuesday, July 16, 2013 1:41 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, December 11, 2013 10:09 AM
Points: 10, 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:
Post #1474274
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse