我有一张像这样的桌子:
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
我希望它看起来像这样:
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
代码:
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)
这将为重复的图书 ID 返回相同的“下一次超时”。而不是 1 个正确值和 1 个空值。
谢谢你!