SQL - Return value from row above

LaLa

I have a table like:

Book        ¦Time Out       ¦Time In 
123456789   ¦01/01/2013 ¦07/07/2013
123456788   ¦15/01/2013 ¦20/01/2013
123456788   ¦23/01/2013 ¦30/01/2013
123144563   ¦01/02/2013 ¦18/02/2013
123144563   ¦20/02/2013 ¦NULL
124567892   ¦03/03/2013 ¦10/03/2013

I would like it to look like this:

Book        ¦Time Out       ¦Time In        ¦Next Time Out
123456789   ¦01/01/2013     ¦07/07/2013     ¦NULL
123456788   ¦15/01/2013     ¦20/01/2013     ¦23/01/2013
123456788   ¦23/01/2013     ¦30/01/2013     ¦NULL
123144563   ¦01/02/2013     ¦18/02/2013     ¦20/02/2013
123144563   ¦20/02/2013     ¦NULL           ¦NULL
124567892   ¦03/03/2013     ¦10/03/2013     ¦NULL

Code:

SELECT nextout.Book,
       nextout.[Time In] AS NextTimeIn
FROM   BookTable nextout
       JOIN BookTable nextoutsec
         ON nextout.Book = nextoutsec.Book
WHERE  nextout.[Time In] = (SELECT MAX(maxtbl.[Time In])
                            FROM   BookTable maxtbl
                            WHERE  maxtbl.Book = nextout.Book) 

This returns for the duplicate book id's the same 'Next Time Out'. rather than 1 correct value and 1 null value.

Thank You!

Lieven Keersmaekers

Untested but something like the following should get you started

;WITH q as (
  SELECT Book, [Time In], ROW_NUMBER() OVER (PARTITION BY Book ORDER BY [Time In]) AS rn
  FROM   BookTable
)
SELECT  bt.*, q2.[Time In] AS NextTimeIn
FROM    BookTable bt
        INNER JOIN q q1 ON q1.Book = bt.Book AND ISNULL(q1.[Time In], 0) = ISNULL(bt.[Time In], 0)
        LEFT OUTER JOIN q q2 ON q2.Book = q1.Book AND q2.rn = q1.rn + 1

The gist of this is

  • q adds a row number to each book, ordered by [Time In]
  • q2 joins with q1 to get the next [Time In] value.
  • q1 joins with BookTable to get all the original values

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Remove rows from Dataframe where row above or below has same value in a specific column

분류에서Dev

Return value if Row(Record) does not Exist in SQL SERVER

분류에서Dev

SQL Return a clean value

분류에서Dev

return value from eval

분류에서Dev

Return value from helper asynchronously?

분류에서Dev

How to get value of cell knowing neighboring cell from same row in sql db?

분류에서Dev

Return string value from sql select command vb.net code behind

분류에서Dev

column width not controlled by row above it

분류에서Dev

SQL insert into row based on column value?

분류에서Dev

SQL Incrementing Value for non existent row

분류에서Dev

SELECT sql row when value changes

분류에서Dev

Find value in Pandas df row and return the column name

분류에서Dev

Find first number greater than x and return another value in that row

분류에서Dev

MySQL return a single row only when all rows are the value of the WHERE

분류에서Dev

Sum value from fields which are in one row

분류에서Dev

SQL QUERY replace NULL value in a row with a another value

분류에서Dev

Getting return value from Task.Run

분류에서Dev

How return value from anonymous method to label?

분류에서Dev

Return as String Value from For Loop Java

분류에서Dev

getting address of pointer from function return value

분류에서Dev

return value from function is always undefined and order

분류에서Dev

Mysql - Return max value from joined table

분류에서Dev

Return generic type from dictionary (else return a default generic value)

분류에서Dev

SQL (MS SQL Server) return column value as the result of the multiplication

분류에서Dev

Join two tables and return only one row from second table

분류에서Dev

Sql delete value from database

분류에서Dev

Smallest expressible value above given value

분류에서Dev

how to subtract between sql row and textbox value php codes?

분류에서Dev

How to return the time() value in Select SQL report query

Related 관련 기사

  1. 1

    Remove rows from Dataframe where row above or below has same value in a specific column

  2. 2

    Return value if Row(Record) does not Exist in SQL SERVER

  3. 3

    SQL Return a clean value

  4. 4

    return value from eval

  5. 5

    Return value from helper asynchronously?

  6. 6

    How to get value of cell knowing neighboring cell from same row in sql db?

  7. 7

    Return string value from sql select command vb.net code behind

  8. 8

    column width not controlled by row above it

  9. 9

    SQL insert into row based on column value?

  10. 10

    SQL Incrementing Value for non existent row

  11. 11

    SELECT sql row when value changes

  12. 12

    Find value in Pandas df row and return the column name

  13. 13

    Find first number greater than x and return another value in that row

  14. 14

    MySQL return a single row only when all rows are the value of the WHERE

  15. 15

    Sum value from fields which are in one row

  16. 16

    SQL QUERY replace NULL value in a row with a another value

  17. 17

    Getting return value from Task.Run

  18. 18

    How return value from anonymous method to label?

  19. 19

    Return as String Value from For Loop Java

  20. 20

    getting address of pointer from function return value

  21. 21

    return value from function is always undefined and order

  22. 22

    Mysql - Return max value from joined table

  23. 23

    Return generic type from dictionary (else return a default generic value)

  24. 24

    SQL (MS SQL Server) return column value as the result of the multiplication

  25. 25

    Join two tables and return only one row from second table

  26. 26

    Sql delete value from database

  27. 27

    Smallest expressible value above given value

  28. 28

    how to subtract between sql row and textbox value php codes?

  29. 29

    How to return the time() value in Select SQL report query

뜨겁다태그

보관