【发布时间】:2015-06-18 07:25:58
【问题描述】:
我有一个查询,它的 select 语句是这样的:
select Greatest(p.price,0) as newprice, sum(q.qty) as qty
from ....
它给了我:
newprice qty
10 1
0 1
100 2
1 2
我想将 newprice 与 qty 相乘得到:
newprice qty result
10 1 10
0 1 0
100 2 200
1 2 2
当我尝试做 select Greatest(p.price,0) as newprice, sum(q.qty) as qty, newprice * qty 它说
ERROR: column "newprice" does not exist
我真的不需要这个额外的专栏。
我真正想要的是:SUM(Greatest(p.price,0) * SUM(q.qty)) 应该给出值 212 但它说 ERROR: aggregate function calls cannot be nested
基本上我只需要将两列相乘并将结果相加。 我知道我可以使用类似于here 所示的 CTE,但我想知道是否有更简单的方法,代码更少。
【问题讨论】:
标签: sql postgresql