如何将表达式中的结果列重用于另一个结果列
示例:
SELECT
(SELECT SUM(...) FROM ...) as turnover,
(SELECT SUM(...) FROM ...) as cost,
turnover - cost as profit
当然这是无效的(至少在 Postgres 中),但是如何在查询中实现相同的效果而不需要重写子查询两次?
Example:
SELECT
(SELECT SUM(...) FROM ...) as turnover,
(SELECT SUM(...) FROM ...) as cost,
turnover - cost as profit
Sure this is invalid (at least in Postgres) but how to achieve the same in a query without rewriting the sub-query twice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
就像这样:
Like so:
您可以像这样重复使用查询:
这相当于:
这里有一点需要注意。第一种方法更具可读性和可重用性,但第二种方法可能更快,因为数据库可能会为其选择更好的计划。
You could reuse the query like this:
This is equivalent to :
There is a point to note here. The first method is more readable and reusable, but the second method might be faster, because the DB might choose a better plan for it.
也许sql“with”子句可以提供帮助,如此处所示 http://orafaq.com/node/1879 (其他数据库如 Postgres 也这样做,不仅仅是 oracle)。
Perhaps the sql "with" clause could help, as presented here http://orafaq.com/node/1879 (other databases such as Postgres do it as well, not just oracle).
实际上,我在这方面做了很多工作,也遇到了很多困难,但最终找到了一个答案 - 更多的是一个黑客 - 但它工作得很好,并将我的查询的读取开销减少了 90%
....与多次复制相关查询以从子查询中检索多个列相比,我只是使用将所有想要返回的值连接到逗号分隔的 varchar 中,然后在应用程序中再次展开它们...
所以
我现在不是
这样做现在拥有我需要的所有三个相关“标量”值,但只需执行一次相关子查询而不是三次。
Actually I did a lot of work on this, and hit many brick walls, but finally figured out an answer - more of a hack - but it worked very well and reduced the read overhead of my queries by 90%....
So rather than duplicating the correlated query many times to retrieve multiple columns from the subquery, I just used concat all the values I want to return into a comma separated varchar, and then unroll them again in the application...
So instead of
I now do
I now have all three correlated 'scalar' values I needed but only had to execute the correlated subquery once instead of three times.
我认为以下方法会起作用:
未在动物身上进行测试 - 你将是第一个! :-)
分享并享受。
I think the following will work:
Not tested on animals - you'll be first! :-)
Share and enjoy.
使用交叉涂抹或外部涂抹。
Use a cross apply or outer apply.
这已经很老了,但我遇到了这个问题并看到了这篇文章,但没有设法使用给定的答案解决我的问题,所以我最终找到了这个解决方案:
如果您的查询是:
您可以将其转换为子查询,然后使用字段例如:
我不完全确定这是否是一种不好的做法,但从性能角度来看,查询超过 224,000 条记录需要 1.5 秒,这对我来说似乎没问题
不确定它后来是否会变成 DB 的同一子查询的 2 倍。
this is pretty old but i ran into this problem and saw this post but didnt manage to solve my problem using the given answers so i eventually arrived at this solution :
if your query is :
you can turn it into a subquery and then use the fields such as :
i'm not entirely sure if this is a bad way of doing things but performance wise it seems okay for me querying over
224,000
records took 1.5 secnot sure if its later on turned into 2x of the same sub query by DB.
您可以使用这样的用户定义变量
http://dev.mysql .com/doc/refman/5.7/en/user-variables.html
You can use user defined variables like this
http://dev.mysql.com/doc/refman/5.7/en/user-variables.html