SELECT
*
FROM
(
SELECT
abcfiles.dbo.diary.BOOKNO,
abcfiles.dbo.BOOKINGS.PARTY,
abcfiles.dbo.BOOKINGS.DEPART,
abcfiles.dbo.BOOKINGS.DEP7,
abcfiles.dbo.BOOKINGS.BOOKED,
abcfiles.dbo.BOOKINGS.BOOK7,
abcfiles.dbo.BOOKINGS.APRICE,
abcfiles.dbo.BOOKINGS.PAID,
abcfiles.dbo.DIARY.ADDED,
abcfiles.dbo.DIARY.ADDED7,
abcfiles.dbo.DIARY.TEXT,
ROW_NUMBER() OVER(PARTITION BY abcfiles.dbo.BOOKINGS.BOOKNO ORDER BY abcfiles.dbo.diary.ADDED DESC) AS RowNum
FROM
abcfiles.dbo.BOOKINGS
INNER JOIN abcfiles.dbo.DIARY ON abcfiles.dbo.BOOKINGS.BOOKNO = abcfiles.dbo.DIARY.BOOKNO
WHERE
(
abcfiles.dbo.BOOKINGS.PAID IS NULL
) OR
(
abcfiles.dbo.BOOKINGS.PAID = 0
)
) t
WHERE
t.RowNum = 1
END
我继续编写abcfiles的原因是因为我需要将存储过程存储在不同的数据库中。
答案 0 :(得分:2)
使用别名(t
已经是别名)。
SELECT * FROM (
SELECT
b.BOOKNO, b.PARTY, b.DEPART, b.DEP7, b.BOOKED, b.BOOK7, b.APRICE, b.PAID,
d.ADDED, d.ADDED7, d.TEXT,
ROW_NUMBER() OVER(PARTITION BY b.BOOKNO ORDER BY d.ADDED DESC) AS RowNum
FROM abcfiles.dbo.BOOKINGS as b
INNER JOIN abcfiles.dbo.DIARY as d ON b.BOOKNO = d.BOOKNO
WHERE (b.PAID IS NULL)
OR (b.PAID = 0)) t
WHERE t.RowNum = 1
as
关键字是可选的。
快乐的SQL'ing。