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开发者_Go百科.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
The reason that I keep writing abcfiles is because I need my stored procedure stored in a different database.
Use aliases (t
was already an alias).
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
The as
keyword is optional.
Happy SQL'ing.
精彩评论