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 2:08 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, December 11, 2013 10:09 AM
Points: 10, Visits: 50
If i upgrade to SQL Server 2012, Can I use the new analytical Functions?
Post #1474286
Posted Tuesday, July 16, 2013 2:22 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 12:26 PM
Points: 280, Visits: 1,782
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.
Post #1474288
Posted Tuesday, July 16, 2013 2:28 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, April 18, 2014 2:24 PM
Points: 11,990, Visits: 11,007
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 Moden's 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)
Post #1474302
Posted Tuesday, July 16, 2013 3:53 PM
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
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


 
Post #1474320
Posted Tuesday, July 16, 2013 4:09 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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


SSC-Forever

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

Group: General Forum Members
Last Login: Yesterday @ 3:30 PM
Points: 41,531, Visits: 34,448
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 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 #1474336
Posted Tuesday, July 16, 2013 4:41 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, April 17, 2014 4:39 AM
Points: 818, Visits: 2,485
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;

Post #1474338
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse