按日期和两个不同表的总和进行分组

发布于 2025-01-10 11:36:23 字数 2413 浏览 0 评论 0原文

给出了以下两个表的摘录 (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、国家/地区的行以及:

  1. 特定订单 ID 的客户在过去 10 年内的订单数量以及30 天
  2. 过去 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:

  1. The amount of orders by the customer of a certain order ID over the last 10 and 30 days
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2025-01-17 11:36:23

您的要求没有明确说明,但我认为您要求提供过去 30 天内每笔订单的清单,以及 10 天内和 30 天内按客户和国家/地区列出的汇总计数。

在 CTE/子查询中
使用客户 ID 将两个表连接在一起
为10天内的订单、30天内的订单添加逻辑列。这些应该返回 true = 1, false = 0

    Select from CTE, 
    use 
    SUM(Within10) OVER(PARTITION BY CustomerID),
    SUM(Within30) OVER(PARTITION BY CountryCode),
    SUM(Within10) OVER(PARTITION BY CustomerID),
    SUM(Within30) OVER(PARTITION BY CountryCode),

这是 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

    Select from CTE, 
    use 
    SUM(Within10) OVER(PARTITION BY CustomerID),
    SUM(Within30) OVER(PARTITION BY CountryCode),
    SUM(Within10) OVER(PARTITION BY CustomerID),
    SUM(Within30) OVER(PARTITION BY CountryCode),

This is T-SQL so the syntax for PL/SQL may vary slightly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文