如何在Postgresql的不同城市获得最新约会
我目前正在尝试立即在PostgreSQL上执行一项任务,在那里我需要在最后预订日期准备一个城市清单,其中还必须包括酒店ID和照片。
这是迄今为止我的SQL查询:
SELECT city.name, booking.booking_date as last_booking_date, hotel.id as hotel_id, hotel.photos ->> 0 as hotel_photo
FROM city
INNER JOIN hotel
ON city.id = hotel.city_id
INNER JOIN booking
ON booking.hotel_id = hotel.id
WHERE
booking.booking_date = (SELECT MAX(booking_date) FROM booking WHERE hotel_id = hotel.id)
ORDER BY city.name ASC;
现在所做的就是返回城市(按字母顺序排序,可以复制),其中还包含其预订日期(也从最早到最新情况)。如果许多酒店都有相同数量的预订,那么请多亏下面的答案,我必须通过ID进行排序
,并且有一些小调整,我能够按字母顺序排列不同的城市,但在他们的最新日期之前却不能返回不同的城市对于某些城市。
SELECT distinct on (city.name)
city.name,
booking.booking_date as last_booking_date,
hotel.id as hotel_id,
hotel.photos ->> 0 as hotel_photo
FROM city
INNER JOIN hotel
ON city.id = hotel.city_id
INNER JOIN booking
ON booking.hotel_id = hotel.id
WHERE
booking.booking_date = (SELECT MAX(booking_date)
FROM booking
WHERE hotel_id = hotel.id)
AND
booking.id = (SELECT MAX(id) FROM booking WHERE hotel_id = hotel.id)
ORDER BY city.name, hotel.id ASC, booking.booking_date ASC;
如您所见,有些城市没有按其最新预订日期进行排序。我尝试了上面的SQL查询的变体:
SELECT distinct on (city.name)
city.name,
booking.booking_date as last_booking_date,
hotel.id as hotel_id,
hotel.photos ->> 0 as hotel_photo
FROM city
INNER JOIN hotel
ON city.id = hotel.city_id
INNER JOIN booking
ON booking.hotel_id = hotel.id
WHERE
booking.booking_date = (SELECT MAX(booking_date)
FROM booking
WHERE hotel_id = hotel.id)
AND
booking.hotel_id = (SELECT MAX(hotel_id) FROM booking WHERE hotel_id = hotel.id)
ORDER BY city.name, hotel.id ASC, booking.booking_date ASC;
但是
SELECT distinct on (city.name)
city.name,
booking.booking_date as last_booking_date,
hotel.id as hotel_id,
hotel.photos ->> 0 as hotel_photo
FROM city
INNER JOIN hotel
ON city.id = hotel.city_id
INNER JOIN booking
ON booking.hotel_id = hotel.id
WHERE
booking.booking_date = (SELECT MAX(booking_date)
FROM booking
WHERE hotel_id = hotel.id)
AND
booking.id = (SELECT MAX(id) FROM booking WHERE hotel_id = hotel.id)
AND
booking.hotel_id = (SELECT MAX(hotel_id) FROM booking WHERE hotel_id = hotel.id)
ORDER BY city.name, hotel.id ASC, booking.booking_date ASC;
它们都显示出与之前的结果相同的结果。
I'm currently trying to do a task on PostgreSQL right now where I need to prepare a list of cities with their date of last reservation which also must include a hotel ID, and a photo.
Here's my SQL query thus far:
SELECT city.name, booking.booking_date as last_booking_date, hotel.id as hotel_id, hotel.photos ->> 0 as hotel_photo
FROM city
INNER JOIN hotel
ON city.id = hotel.city_id
INNER JOIN booking
ON booking.hotel_id = hotel.id
WHERE
booking.booking_date = (SELECT MAX(booking_date) FROM booking WHERE hotel_id = hotel.id)
ORDER BY city.name ASC;
Now what this does is return the cities (sorted in alphabetical order, and can duplicate) which also contains their booking dates (also sorted from earliest to latest). If many hotels have the same amount of bookings, then I'll have to sort them by ID
Thanks to the answer below, and with some minor tweaks, I was able to return the distinct cities in alphabetical order, but not by their latest date for some cities.
SELECT distinct on (city.name)
city.name,
booking.booking_date as last_booking_date,
hotel.id as hotel_id,
hotel.photos ->> 0 as hotel_photo
FROM city
INNER JOIN hotel
ON city.id = hotel.city_id
INNER JOIN booking
ON booking.hotel_id = hotel.id
WHERE
booking.booking_date = (SELECT MAX(booking_date)
FROM booking
WHERE hotel_id = hotel.id)
AND
booking.id = (SELECT MAX(id) FROM booking WHERE hotel_id = hotel.id)
ORDER BY city.name, hotel.id ASC, booking.booking_date ASC;
This has the following result, with the expected result shown as well.
As you can see, some cities are not sorted by their latest date of booking. I've tried variations of the SQL query above such as:
SELECT distinct on (city.name)
city.name,
booking.booking_date as last_booking_date,
hotel.id as hotel_id,
hotel.photos ->> 0 as hotel_photo
FROM city
INNER JOIN hotel
ON city.id = hotel.city_id
INNER JOIN booking
ON booking.hotel_id = hotel.id
WHERE
booking.booking_date = (SELECT MAX(booking_date)
FROM booking
WHERE hotel_id = hotel.id)
AND
booking.hotel_id = (SELECT MAX(hotel_id) FROM booking WHERE hotel_id = hotel.id)
ORDER BY city.name, hotel.id ASC, booking.booking_date ASC;
and
SELECT distinct on (city.name)
city.name,
booking.booking_date as last_booking_date,
hotel.id as hotel_id,
hotel.photos ->> 0 as hotel_photo
FROM city
INNER JOIN hotel
ON city.id = hotel.city_id
INNER JOIN booking
ON booking.hotel_id = hotel.id
WHERE
booking.booking_date = (SELECT MAX(booking_date)
FROM booking
WHERE hotel_id = hotel.id)
AND
booking.id = (SELECT MAX(id) FROM booking WHERE hotel_id = hotel.id)
AND
booking.hotel_id = (SELECT MAX(hotel_id) FROM booking WHERE hotel_id = hotel.id)
ORDER BY city.name, hotel.id ASC, booking.booking_date ASC;
But they all show the same result as the one earlier.
With that, how do I get the row with the latest date? For context, this is what the first query returns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
前几天,我看到了一个类似的问题,类似这样的问题
写了一个查询来检索城市名称,该城市最新预订的日期是最受欢迎的酒店的ID(这是最多的酒店的酒店预订),以及每个城市的那家酒店的照片。结果应按城市名称按升序排序。如果两家酒店的预订数量相同,则应按酒店ID进行排序。
问题声明
数据库架构
描述
您的任务是准备一个城市中最后预订日期的城市清单这座城市最受欢迎的(按预订数量)酒店的主要照片(照片[0])。
按城市按顺序排序的结果。如果许多酒店的预订数量相同,则按ID(上升顺序)对其进行排序。
请记住,查询也将运行不同的数据集。
示例输出
示例数据
表:城市
表:酒店
表:预订
预期输出
解决方案
I saw a similar problem the other day something like this
Write a query to retrieve the city name, the date of the most recent reservation in that city, the hotel ID of the most popular hotel (the one with the highest number of bookings), and a photo of that hotel for each city. The results should be sorted in ascending order by the city name. If two hotels have the same number of bookings, they should be sorted in ascending order by hotel ID.
Problem Statement
Database Schema
DESCRIPTION
Your task is to prepare a list of cities with the date of last reservation made in the city and a main photo (photos[0]) of the most popular (by number of bookings) hotel in this city.
Sort results in ascending order by city. If many hotels have the same amount of bookings sort them by ID (ascending order).
Remember that the query will also be run of different datasets.
Example Output
Sample Data
Table: city
Table: hotel
Table: booking
Expected output
Solution
在上使用
。根据
子句的顺序中的顺序,它将选择子集的第一个元素。例如:
Use
DISTINCT ON
. It will pick the first element for the subset, according to the ordering in theORDER BY
clause. For example:我相信,您不需要子查询。
您的按声明订单略有关闭:它首先按城市分类,然后是酒店,然后才选择约会。
因此,如果您修改订单按City.Name,日期desc,然后是Hotel_id,则应进行工作。
找到以下代码:
I believe, you do not need the WHEN subquery.
Your ORDER BY statement is slightly off: it first sorts by city, then by hotel and only then choses a date.
So, if you modify ORDER BY to be by city.name, date DESC and then hotel_id it should make the work.
Find the code below: