sql query to get column of the next row to be of column of a row

stkflmb12

I want to write a sql query to get column of the next row to be of column of a row.the test example is as follow: table:

ID   startno 
1       1             
2       5           
3       9           

I want to get sql query to get result as follow:

ID   startno  endno
1      1         5
2      5         9
3      9         null
Raging Bull

You can do it this way:

WITH CTE AS
(SELECT *,ROW_NUMBER()OVER(ORDER BY ID) AS RN
FROM TableName)

SELECT T1.ID,T1.startno,T2.startno as endno
FROM CTE T1 LEFT JOIN
     CTE T2 ON T1.RN=(T2.RN-1)

You can use ON T1.ID=(T2.ID-1) as well. But if the ID field is not continuous or missing any ID, Join won't work as we exptected. That is why I have used ROW_NUMBER to get a continuous series of numbers to join the tables with.

Result:

ID  startno endno
1   1       5
2   5       9
3   9       (null)

Sample result in SQL Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL query to get column of the next row to be of column of a row and the second row of the column to the next row

From Dev

sql query for grouping column into row

From Dev

Seperate row in column with SQL query

From Dev

How to get next row column by javascript or jquery

From Dev

sql query get multiple values from same column for one row

From Dev

SQL query to get computed values from the same column in one row

From Dev

Get same column in same row two times in SQL query

From Dev

Sql query to get 1 from each column row wise

From Dev

SQL Query to Transpose Column Counts to Row Counts

From Dev

Multiple CSV Column to Row SQL Query

From Dev

How to pivot a column into a row for sql query

From Dev

Get MAX from row with column name (SQL)

From Dev

SQL Pivot and get column row data

From Dev

How to get data of an specefic row or column in sql

From Dev

get row and column depending on value using SQL?

From Dev

Show different row and column data in one column using SQL query

From Dev

Get a value of a row in a column

From Dev

SQL Pivot row to column?

From Dev

Subtract column with the next column in a different row

From Dev

adding a value to a column from data in next row sql

From Dev

SQL query to get the Sum of all column values in the last row in access table

From Dev

how to get sql select query result as column vs value wise instead of row wise without using PIVOT

From Dev

Query a column for row values in HDFS

From Dev

MySQL query - get row with highest number filtered by another column

From Dev

MySQL QUERY to get row where column ellipse number >= 1

From Dev

Mysql Query - Get row value based on column name in a different table

From Dev

VB.Net Exception how to get the row and column of error in query

From Dev

Oracle Sql query to return the column result in single row

From Dev

sql query to convert existing column entries into row headers

Related Related

  1. 1

    SQL query to get column of the next row to be of column of a row and the second row of the column to the next row

  2. 2

    sql query for grouping column into row

  3. 3

    Seperate row in column with SQL query

  4. 4

    How to get next row column by javascript or jquery

  5. 5

    sql query get multiple values from same column for one row

  6. 6

    SQL query to get computed values from the same column in one row

  7. 7

    Get same column in same row two times in SQL query

  8. 8

    Sql query to get 1 from each column row wise

  9. 9

    SQL Query to Transpose Column Counts to Row Counts

  10. 10

    Multiple CSV Column to Row SQL Query

  11. 11

    How to pivot a column into a row for sql query

  12. 12

    Get MAX from row with column name (SQL)

  13. 13

    SQL Pivot and get column row data

  14. 14

    How to get data of an specefic row or column in sql

  15. 15

    get row and column depending on value using SQL?

  16. 16

    Show different row and column data in one column using SQL query

  17. 17

    Get a value of a row in a column

  18. 18

    SQL Pivot row to column?

  19. 19

    Subtract column with the next column in a different row

  20. 20

    adding a value to a column from data in next row sql

  21. 21

    SQL query to get the Sum of all column values in the last row in access table

  22. 22

    how to get sql select query result as column vs value wise instead of row wise without using PIVOT

  23. 23

    Query a column for row values in HDFS

  24. 24

    MySQL query - get row with highest number filtered by another column

  25. 25

    MySQL QUERY to get row where column ellipse number >= 1

  26. 26

    Mysql Query - Get row value based on column name in a different table

  27. 27

    VB.Net Exception how to get the row and column of error in query

  28. 28

    Oracle Sql query to return the column result in single row

  29. 29

    sql query to convert existing column entries into row headers

HotTag

Archive