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
keebler96
keebler96
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

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

Group: General Forum Members
Points: 12 Visits: 50
The rownumber is very costly on a large table,

The Order By can be By Cost_Center_Code, Payment_Code, Inv_No

Thanks
Khalid Hanif-458693
Khalid Hanif-458693
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 50
If i upgrade to SQL Server 2012, Can I use the new analytical Functions?
patrickmcginnis59 10839
patrickmcginnis59 10839
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1725 Visits: 5485
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.

to properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
Khalid Hanif-458693
Khalid Hanif-458693
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 50
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.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25767 Visits: 17509
Khalid Hanif-458693 (7/16/2013)
The rownumber is very costly on a large table,

The Order By can be By Cost_Center_Code, Payment_Code, Inv_No

Thanks


You are going to have to use RowNumber somewhere along the way to know which row is previous. Maybe you should try it before you simply refuse it. Unless you are numbering the entire table it really is not a big deal.

From what I am interpreting you would want to partition by Cost_Center_Code, Payment_Code, Inv_No and Order by Payment_Code.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Steven Willis
Steven Willis
SSC Eights!
SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)

Group: General Forum Members
Points: 813 Visits: 1721
I don't see how you can force the order without using an ordering column of some kind so it seems the easiest solution is to just add an IDENTITY column to the target table. Then we can query the data in the order it was entered. (You could probably also use a TIMESTAMP column, a DATETIME, or a NEWSEQUENTIALID for ordering as well...but you will need something that indicates the order of the rows since SQL doesn't necessarily store them in the order they are entered.)

With that addition then, this query will do what you want:



--Sample data

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

CREATE TABLE #Invoice_t (
[RowID] INT IDENTITY(1,1) NOT NULL,
[Cost_Center_code] [int] NOT NULL,
[Payment_code] [int] NOT NULL,
[INV_No] [int] NOT NULL,
PRIMARY KEY CLUSTERED ([RowID],[Cost_Center_code],[Payment_code],[INV_No]))

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)

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)






SET NOCOUNT ON

;WITH
cteInvoices
AS
(
SELECT
RowID
,RowID+1 AS PrevRowID
,RowID-1 AS NextRowID
,Cost_Center_code
,Payment_code
,INV_No
FROM
#Invoice_t
)
SELECT
i.Cost_Center_code
,i.Payment_code
,i.INV_No
,p.Cost_Center_code AS [prev_cost_center_code]
,p.Payment_code AS [prev_payment_code]
,p.INV_No AS [prev_Inv_no]
,n.Cost_Center_code AS [next_cost_center_code]
,n.Payment_code AS [next_payment_code]
,n.INV_No AS [next_Inv_no]
FROM
cteInvoices AS i
LEFT OUTER JOIN
cteInvoices AS p
ON i.RowID = p.PrevRowID
LEFT OUTER JOIN
cteInvoices AS n
ON i.RowID = n.NextRowID
WHERE
i.RowID > 0




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

Group: General Forum Members
Points: 12 Visits: 50
If the use the identity column, it will mess up the order since sql will order it differently.

I need data ordered by how sql orders it.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86028 Visits: 45226
Khalid Hanif-458693 (7/16/2013)
I need data ordered by how sql orders it.


SQL doesn't order it, that's what people are trying to tell you.
There is no default ordering. There is no concept of order of inserts. There is no implied order of rows in a table.

If you want any concept of 'previous', 'next', or 'order', you need to define that order based on a column in the table.

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


mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1556 Visits: 3317
Khalid Hanif-458693 (7/16/2013)
If i upgrade to SQL Server 2012, Can I use the new analytical Functions?


You could use the new functions in 2012 to do this, but I'm not sure that this would be any cheaper than using a ROW_NUMBER()
SELECT
i.Cost_Center_code
,i.Payment_code
,i.INV_No
,LAG(i.Cost_Center_code, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [prev_cost_center_code]
,LAG(i.Payment_code, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [prev_payment_code]
,LAG(i.INV_No, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [prev_Inv_no]
,LEAD(i.Cost_Center_code, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [next_cost_center_code]
,LEAD(i.Payment_code, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [next_payment_code]
,LEAD(i.INV_No, 1) OVER (ORDER BY Cost_Center_Code, Payment_Code, Inv_No) AS [next_Inv_no]
FROM
#Invoice_t AS i;


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