按日期和两个不同表的总和进行分组
给出了以下两个表的摘录 (Oracle SQL):
+----------+------------+-------------+
| Orders | | |
+----------+------------+-------------+
| Order ID | Date | Customer ID |
| 12345 | 12.05.2018 | 456 |
| 12346 | 01.09.2021 | 646 |
| 12347 | 03.03.2019 | 836 |
| 12348 | 04.06.2020 | 1026 |
| 12349 | 05.07.2020 | 1216 |
| 12350 | 04.01.2020 | 1406 |
+----------+------------+-------------+
+-------------+----------+
| Country | |
+-------------+----------+
| Customer ID | Country |
| 1026 | GB |
| 836 | USA |
| 1026 | Germany |
| 2166 | USA |
| 2546 | GB |
| 4154 | France |
+-------------+----------+
期望的结果应提供包含订单 ID、日期、客户 ID、国家/地区的行以及:
- 特定订单 ID 的客户在过去 10 年内的订单数量以及30 天
- 过去 10 和 30 天内某个客户所在国家/地区的订单金额
由于每个客户都属于一个国家/地区,因此按国家/地区汇总的金额始终至少与客户的订单金额一样高。
所以结果应该是这样的:
+--------------------------------------+------------+-------------+------------+-------------------------------------+-------------------------------------+------------------------------------+------------------------------------+
| Desired Outcome (Results fictitious) | | | | | | | |
+--------------------------------------+------------+-------------+------------+-------------------------------------+-------------------------------------+------------------------------------+------------------------------------+
| Order ID | Date | Customer ID | Country ID | Amount Orders Cutsomer Last 10 Days | Amount Orders Cutsomer Last 30 Days | Amount Orders Country Last 10 Days | Amount Orders Country Last 30 Days |
| 12347 | 03.03.2019 | 836 | USA | 7 | 15 | 124 | 578 |
+--------------------------------------+------------+-------------+------------+-------------------------------------+-------------------------------------+------------------------------------+------------------------------------+
The following extracts of two tables are given (Oracle SQL):
+----------+------------+-------------+
| Orders | | |
+----------+------------+-------------+
| Order ID | Date | Customer ID |
| 12345 | 12.05.2018 | 456 |
| 12346 | 01.09.2021 | 646 |
| 12347 | 03.03.2019 | 836 |
| 12348 | 04.06.2020 | 1026 |
| 12349 | 05.07.2020 | 1216 |
| 12350 | 04.01.2020 | 1406 |
+----------+------------+-------------+
+-------------+----------+
| Country | |
+-------------+----------+
| Customer ID | Country |
| 1026 | GB |
| 836 | USA |
| 1026 | Germany |
| 2166 | USA |
| 2546 | GB |
| 4154 | France |
+-------------+----------+
The desired outcome should provide lines with Order ID, Date, Customer ID, Country as well as:
- The amount of orders by the customer of a certain order ID over the last 10 and 30 days
- The amount of orders by the country of a certain customer over the last 10 and 30 days
Since every customer belongs to a country the aggregated amounty by country are always at least as high as by a customer.
So the result should look like this:
+--------------------------------------+------------+-------------+------------+-------------------------------------+-------------------------------------+------------------------------------+------------------------------------+
| Desired Outcome (Results fictitious) | | | | | | | |
+--------------------------------------+------------+-------------+------------+-------------------------------------+-------------------------------------+------------------------------------+------------------------------------+
| Order ID | Date | Customer ID | Country ID | Amount Orders Cutsomer Last 10 Days | Amount Orders Cutsomer Last 30 Days | Amount Orders Country Last 10 Days | Amount Orders Country Last 30 Days |
| 12347 | 03.03.2019 | 836 | USA | 7 | 15 | 124 | 578 |
+--------------------------------------+------------+-------------+------------+-------------------------------------+-------------------------------------+------------------------------------+------------------------------------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的要求没有明确说明,但我认为您要求提供过去 30 天内每笔订单的清单,以及 10 天内和 30 天内按客户和国家/地区列出的汇总计数。
在 CTE/子查询中
使用客户 ID 将两个表连接在一起
为10天内的订单、30天内的订单添加逻辑列。这些应该返回 true = 1, false = 0
这是 T-SQL,因此 PL/SQL 的语法可能略有不同
Your requirements are not clearly stated but I think you are asking for a list of every order within last 30 days and a summary count by Customer and Country for within 10 and within 30 days.
In a CTE/SubQuery
Join the two table together using customer ID
add logical columns for order within 10 days, order within 30 days. These should return true = 1, false = 0
This is T-SQL so the syntax for PL/SQL may vary slightly