嵌套查询 - 寻找更好的解决方案
考虑一个销售应用程序,其中有两个表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要聚合两次,并且联合运算符通常会执行隐式唯一排序,这在您的情况下也是不必要的。
删除中间聚合和唯一排序应该会有所帮助。
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.
Removing the intermediate aggregation and the unique sort should help a bit.