嵌套查询 - 寻找更好的解决方案

发布于 2024-11-28 18:42:28 字数 1546 浏览 0 评论 0原文

考虑一个销售应用程序,其中有两个表 SALES 和 INTERNAL_SALES。

  • 表“SALES”引用了公司外部每个销售人员进行的交易数量。
  • 表“INTERNAL_SALES”引用公司内部每个销售人员与其他销售人员进行的交易数量。

销售表: 即使交易为零,每个日期都有针对每个销售人员的一个条目。

id |  day          | sales_person | number_of_transactions
1  | 2011-08-01    | Tom          | 1000
2  | 2011-08-01    | Ben          | 500
3  | 2011-08-01    | Anne         | 1500
4  | 2011-08-02    | Tom          | 0
5  | 2011-08-02    | Ben          | 800
6  | 2011-08-02    | Anne         | 900
7  | 2011-08-03    | Tom          | 3000
8  | 2011-08-03    | Ben          | 0
9  | 2011-08-03    | Anne         | 40

INTERNAL_SALES 表: 该表仅记录销售人员之间实际进行的交易。

id | day        | sales_person_from | sales_person_to | number_of_transactions
 0 | 2011-08-01 | Tom               | Ben             | 10
 1 | 2011-08-01 | Tom               | Anne            | 20
 2 | 2011-08-01 | Ben               | Tom             | 50
 3 | 2011-08-03 | Anne              | Tom             | 30
 4 | 2011-08-03 | Anne              | Tom             | 30

现在的问题是得出每个销售人员每天的总交易量。我这样做的方法是:

SELECT day, sales_person, sum(num_transactions) from
(
  SELECT day, sales_person, number_of_transactions As num_transactions FROM sales;
  UNION
  SELECT day, sales_person_from As sales_person, sum(number_of_transactions) As num_transactions FROM internal_sales GROUP BY day, sales_person_from;
)
GROUP BY day, sales_person;

这太慢而且看起来很丑。我正在寻求更好的解决方案。顺便说一句,Oracle 中使用的数据库除了可以对其运行查询外,我无法控制数据库。

Consider a sales application where we have two tables SALES and INTERNAL_SALES.

  • Table "SALES" references the number of transactions made by each sales person outside the company.
  • Table "INTERNAL_SALES" references the number of transactions made by each sales person inside the company to another sales person.

SALES Table:
Each date has one entry against each sales person even if transactions are zero.

id |  day          | sales_person | number_of_transactions
1  | 2011-08-01    | Tom          | 1000
2  | 2011-08-01    | Ben          | 500
3  | 2011-08-01    | Anne         | 1500
4  | 2011-08-02    | Tom          | 0
5  | 2011-08-02    | Ben          | 800
6  | 2011-08-02    | Anne         | 900
7  | 2011-08-03    | Tom          | 3000
8  | 2011-08-03    | Ben          | 0
9  | 2011-08-03    | Anne         | 40

INTERNAL_SALES Table:
This table logs only the transactions that were actually made between sales persons.

id | day        | sales_person_from | sales_person_to | number_of_transactions
 0 | 2011-08-01 | Tom               | Ben             | 10
 1 | 2011-08-01 | Tom               | Anne            | 20
 2 | 2011-08-01 | Ben               | Tom             | 50
 3 | 2011-08-03 | Anne              | Tom             | 30
 4 | 2011-08-03 | Anne              | Tom             | 30

Now the problem is to come up with total transactions by each sales person on a daily basis. The way I did this is:

SELECT day, sales_person, sum(num_transactions) from
(
  SELECT day, sales_person, number_of_transactions As num_transactions FROM sales;
  UNION
  SELECT day, sales_person_from As sales_person, sum(number_of_transactions) As num_transactions FROM internal_sales GROUP BY day, sales_person_from;
)
GROUP BY day, sales_person;

This is too slow and looks ugly. I am seeking a better solution. By the way the database being used in Oracle and I have no control over database except that I can run queries against it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半城柳色半声笛 2024-12-05 18:42:28

不需要聚合两次,并且联合运算符通常会执行隐式唯一排序,这在您的情况下也是不必要的。

SELECT day, sales_person, sum(num_transactions) from
(
    SELECT day, sales_person, number_of_transactions As num_transactions FROM sales;
    UNION ALL 
    SELECT day, sales_person_from, number_of_transactions FROM internal_sales;
)
GROUP BY day, sales_person;

删除中间聚合和唯一排序应该会有所帮助。

There is no need to aggregate twice, and the union operator typically does an implicit unique sort which, again, in not necessary in your case.

SELECT day, sales_person, sum(num_transactions) from
(
    SELECT day, sales_person, number_of_transactions As num_transactions FROM sales;
    UNION ALL 
    SELECT day, sales_person_from, number_of_transactions FROM internal_sales;
)
GROUP BY day, sales_person;

Removing the intermediate aggregation and the unique sort should help a bit.

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