加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 16:26:39 所属栏目:百科 来源:网络整理
导读:在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 tot
在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_3
from data;

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

the column total_1 and total_2 do not exist.

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

不会有任何性能损失.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读