PostgreSQL:在同一查询中使用计算列

PostgreSQL:在同一查询中使用计算列,第1张

概述在postgres中使用计算列时遇到麻烦.下面给出了一个类似于SQL的代码,可以在PostgreSQL中重新创建吗? select cost_1, quantity_1, cost_2, quantity_2, (cost_1 * quantity_1) as total_1, (cost_2 * quantity_2) as total_2, (calcul 在postgres中使用计算列时遇到麻烦.下面给出了一个类似于sql的代码,可以在Postgresql中重新创建吗?
select cost_1,quantity_1,cost_2,quantity_2,(cost_1 * quantity_1) as total_1,(cost_2 * quantity_2) as total_2,(calculated total_1 + calculated total_2) as total_3from data;

在Postgresql中,类似的代码返回错误:

the column total_1 and total_2 do not exist.

您需要将SELECT语句包装到派生表中才能访问列别名:
select cost1,quantity_2       total_1 + total_2 as total_3from (    select cost_1,(cost_2 * quantity_2) as total_2    from data) t

不会有任何性能损失.

(我真的很惊讶你的原始SQL语句在DBMS中运行)

总结

以上是内存溢出为你收集整理的PostgreSQL:在同一查询中使用计算列全部内容,希望文章能够帮你解决PostgreSQL:在同一查询中使用计算列所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1169309.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存