帮助查询只返回最近的记录
我正在尝试创建一个简单的视图,从两个数据库获取数据。
我有一个 booking
表,仅显示每个预订一次,还有一个日记表,用于记录每个预订。
每个预订都会在日记表中多次显示,但我只希望在我的视图中看到最新的注释和预订信息
出于某种原因,这不起作用
SELECT
t0.BOOKNO, t0.PARTY, t0.TOUR, t0.DEPART, t0.DEP7, t0.BOOK7, t0.PAID, t0.APRICE, t1.TEXT
FROM
(SELECT DISTINCT
TOP (100) PERCENT
dbo.DIARY.ADDED, dbo.BOOKINGS.BOOKNO, dbo.BOOKINGS.PARTY,
dbo.BOOKINGS.TOUR, dbo.BOOKINGS.DEPART,
dbo.BOOKINGS.DEP7, dbo.BOOKINGS.BOOKED, dbo.BOOKINGS.BOOK7, dbo.BOOKINGS.PAID,
dbo.BOOKINGS.APRICE, dbo.BOOKINGS.STATUS
FROM
dbo.BOOKINGS
CROSS JOIN
dbo.DIARY
WHERE
(dbo.BOOKINGS.PAID IS NULL)
OR (dbo.BOOKINGS.PAID = 0)
GROUP BY
dbo.BOOKINGS.BOOKNO, dbo.BOOKINGS.PARTY, dbo.BOOKINGS.TOUR, dbo.BOOKINGS.DEPART,
dbo.BOOKINGS.DEP7, dbo.BOOKINGS.BOOKED, dbo.BOOKINGS.APRICE, dbo.BOOKINGS.STATUS,
dbo.BOOKINGS.BOOK7, dbo.BOOKINGS.PAID, dbo.DIARY.ADDED
HAVING
(dbo.BOOKINGS.STATUS <> 'xl') AND
(dbo.BOOKINGS.DEPART > CONVERT(int, GETDATE()))
ORDER BY
dbo.BOOKINGS.BOOKNO) AS t0
INNER JOIN
(SELECT
BOOKNO, TEXT, MAX(ADDED) AS added
FROM
dbo.DIARY AS DIARY_1
GROUP BY BOOKNO, TEXT) AS t1 ON t0.BOOKNO = t1.BOOKNO
我尝试了此操作,但它不起作用无法解析查询文本?
;WITH MostRecentBookings AS ( SELECT dbo.DIARY.ADDED7, dbo.DIARY.ADDED, dbo.BOOKINGS.BOOKNO, dbo.BOOKINGS.PARTY, dbo.BOOKINGS.TOUR, dbo.BOOKINGS.DEPART,
dbo.BOOKINGS.DEP7, dbo.BOOKINGS.BOOKED, dbo.BOOKINGS.BOOK7, dbo.BOOKINGS.PAID, dbo.BOOKINGS.APRICE, dbo.BOOKINGS.STATUS,
dbo.DIARY.TEXT, ROW_NUMBER() OVER(PARTITION BY dbo.BOOKINGS.BOOKNO ORDER BY dbo.DIARY.ADDED DESC) AS 'RowNum'
FROM dbo.BOOKINGS INNER JOIN dbo.DIARY ON dbo.BOOKINGS.BOOKNO = dbo.DIARY.BOOKNO
WHERE (dbo.BOOKINGS.PAID IS NULL) OR (dbo.BOOKINGS.PAID = 0))
SELECT * FROM MostRecentBookings
WHERE RowNum = 1
I am trying to create a simple view, that gets data from 2 databases.
I have a booking
table that only shows each booking once and a diary table that is notes on each booking.
Each booking shows up multiple times in the diary table however I only want the most recent note and booking information in my view
For some reason this isn't working
SELECT
t0.BOOKNO, t0.PARTY, t0.TOUR, t0.DEPART, t0.DEP7, t0.BOOK7, t0.PAID, t0.APRICE, t1.TEXT
FROM
(SELECT DISTINCT
TOP (100) PERCENT
dbo.DIARY.ADDED, dbo.BOOKINGS.BOOKNO, dbo.BOOKINGS.PARTY,
dbo.BOOKINGS.TOUR, dbo.BOOKINGS.DEPART,
dbo.BOOKINGS.DEP7, dbo.BOOKINGS.BOOKED, dbo.BOOKINGS.BOOK7, dbo.BOOKINGS.PAID,
dbo.BOOKINGS.APRICE, dbo.BOOKINGS.STATUS
FROM
dbo.BOOKINGS
CROSS JOIN
dbo.DIARY
WHERE
(dbo.BOOKINGS.PAID IS NULL)
OR (dbo.BOOKINGS.PAID = 0)
GROUP BY
dbo.BOOKINGS.BOOKNO, dbo.BOOKINGS.PARTY, dbo.BOOKINGS.TOUR, dbo.BOOKINGS.DEPART,
dbo.BOOKINGS.DEP7, dbo.BOOKINGS.BOOKED, dbo.BOOKINGS.APRICE, dbo.BOOKINGS.STATUS,
dbo.BOOKINGS.BOOK7, dbo.BOOKINGS.PAID, dbo.DIARY.ADDED
HAVING
(dbo.BOOKINGS.STATUS <> 'xl') AND
(dbo.BOOKINGS.DEPART > CONVERT(int, GETDATE()))
ORDER BY
dbo.BOOKINGS.BOOKNO) AS t0
INNER JOIN
(SELECT
BOOKNO, TEXT, MAX(ADDED) AS added
FROM
dbo.DIARY AS DIARY_1
GROUP BY BOOKNO, TEXT) AS t1 ON t0.BOOKNO = t1.BOOKNO
I tryed this but it didnt work unable to parse query text?
;WITH MostRecentBookings AS ( SELECT dbo.DIARY.ADDED7, dbo.DIARY.ADDED, dbo.BOOKINGS.BOOKNO, dbo.BOOKINGS.PARTY, dbo.BOOKINGS.TOUR, dbo.BOOKINGS.DEPART,
dbo.BOOKINGS.DEP7, dbo.BOOKINGS.BOOKED, dbo.BOOKINGS.BOOK7, dbo.BOOKINGS.PAID, dbo.BOOKINGS.APRICE, dbo.BOOKINGS.STATUS,
dbo.DIARY.TEXT, ROW_NUMBER() OVER(PARTITION BY dbo.BOOKINGS.BOOKNO ORDER BY dbo.DIARY.ADDED DESC) AS 'RowNum'
FROM dbo.BOOKINGS INNER JOIN dbo.DIARY ON dbo.BOOKINGS.BOOKNO = dbo.DIARY.BOOKNO
WHERE (dbo.BOOKINGS.PAID IS NULL) OR (dbo.BOOKINGS.PAID = 0))
SELECT * FROM MostRecentBookings
WHERE RowNum = 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,您可以使用 CTE(通用表表达式)与
ROW_NUMBER()
函数相结合来实现此目的。类似于:基本上,CTE 会在两个表之间进行连接,然后按
BOOK_NO
对数据进行“分区” - 这样,每个唯一的预订都会获得一个从 1 开始的行号。编号方式是最近的条目编号为 1,前一个条目编号为后续更高的编号。从该 CTE 中,选择
RowNum = 1
的条目作为最近预订的列表。Basically, you could use a CTE (Common Table Expression) combined with the
ROW_NUMBER()
function to achieve this. Something like:Basically, the CTE would do the join between the two tables, and it would then "partition" your data by
BOOK_NO
- this way, each unique booking gets a row number that starts at 1.The numbering is such that the most recent entry gets number 1, and previous one gets subsequent higher numbers. From that CTE, select those entries with
RowNum = 1
for the list of the most recent bookings.