ms-access - 在具有多列的访问数据库中排序
问题描述
我试图在访问中对数据库进行排序,但我无法完成。首先是价格最低的城市和该城市的所有价格,然后是价格第二低的城市,以此类推。
这是示例数据库:
Country City Price Departure_date Return_date
Peru Lima 360$ xxxx xxxxx
Peru Lima 420$ xxxx xxxxx
Mexico CMX 300$ xxxx xxxxx
Mexico CMX 400$ xxxx xxxxx
Mexico Cancun 350$ xxxx xxxxx
Mexico Cancun 500$ xxxx xxxxx
Peru Cusco 50$ xxxx xxxxx
Peru Cusco 60$ xxxx xxxxx
它必须以这种方式排序:
Country City Price Departure_date Return_date
Peru Cusco 50$ xxxx xxxxx
Peru Cusco 60$ xxxx xxxxx
Mexico CMX 300$ xxxx xxxxx
Mexico CMX 400$ xxxx xxxxx
Mexico Cancun 350$ xxxx xxxxx
Mexico Cancun 500$ xxxx xxxxx
Peru Lima 360$ xxxx xxxxx
Peru Lima 420$ xxxx xxxxx
第二部分:
我需要它只使用最新日期的行。
这是示例数据库:
Date Country City Price Departure_date Return_date
05-06-2019 Peru Lima 360$ xxxx xxxxx
05-06-2019 Peru Lima 420$ xxxx xxxxx
05-06-2019 Mexico CMX 300$ xxxx xxxxx
05-06-2019 Mexico CMX 400$ xxxx xxxxx
05-06-2019 Mexico Cancun 350$ xxxx xxxxx
05-06-2019 Mexico Cancun 500$ xxxx xxxxx
05-06-2019 Peru Cusco 50$ xxxx xxxxx
05-06-2019 Peru Cusco 60$ xxxx xxxxx
04-06-2017 Mexico Cancun 300$ xxxx xxxxx
04-06-2017 Peru Cusco 70$ xxxx xxxxx
04-06-2017 Peru Cusco 30$ xxxx xxxxx
它必须以这种方式排序:
Date Country City Price Departure_date Return_date
05-06-2019 Peru Cusco 50$ xxxx xxxxx
05-06-2019 Peru Cusco 60$ xxxx xxxxx
05-06-2019 Mexico CMX 300$ xxxx xxxxx
05-06-2019 Mexico CMX 400$ xxxx xxxxx
05-06-2019 Mexico Cancun 350$ xxxx xxxxx
05-06-2019 Mexico Cancun 500$ xxxx xxxxx
05-06-2019 Peru Lima 360$ xxxx xxxxx
05-06-2019 Peru Lima 420$ xxxx xxxxx
解决方案
尝试这个:
SELECT t1.*
FROM Table1 AS t1 INNER JOIN
(SELECT Table1.City, Min(Table1.Price) AS min_price
FROM Table1
GROUP BY Table1.City) AS t2
ON t1.City = t2.City
ORDER BY t2.min_price, t1.City, t1.Price
为了适应第二部分,包括 WHERE 子句:
SELECT t1.*
FROM Table1 AS t1 INNER JOIN
(SELECT Table1.City, Min(Table1.Price) AS min_price
FROM Table1
WHERE DateEnter=(SELECT Max(DateEnter) AS MaxDate FROM Table1)
GROUP BY Table1.City) AS t2
ON t1.City = t2.City
WHERE DateEnter=(SELECT Max(DateEnter) AS MaxDate FROM Table1)
ORDER BY t2.min_price, t1.City, t1.Price;
推荐阅读
- google-chrome-extension - 创建chrome窗口时如何检测键盘事件?
- shell - 使用外壳/批处理解压缩 1 个 zip 文件中的多个特定文件夹的最佳方法
- python - 为什么列表和元组的“in”行为不同?
- python - 计算一个类的函数被执行的次数
- mongodb - 使用 Rust Mongo 驱动程序原型时,如何将 chrono::DateTime 字段序列化为 ISODate?
- c - 从字符串中删除括号中的文本?
- r - 使用 ggcontour 在数据集上叠加树
- javascript - 阻止对文件 css 和 javascript 的直接访问
- ios - Objective-C 中方法 [DJIRemoteController setCustomButtonTags:withCompletion] 的标签中可以使用哪些值?
- java - JUnit测试一个void方法?