下面给出的是我要根据 productid
+-----------+-----------+----------+
'productid | free | qty |
'------------+-----------+----------+
' 2372 | 0 | 100 |
'------------+-----------+----------+
' 2311 | 0 | 100 |
'------------+-----------+----------+
' 2311 | 1 | 10 |
'------------------------------------
我需要得到以下结果
+----------- +-----------+
'productid | toqty |
'------------+-----------+
' 2372 | 100 |
'------------+-----------+
' 2311 | 110 |
'-------------------------+
我试过的是。
select sum(qty) as totQty
from tmpprch
where productid=(select productid from tmpprch)
并卡在这里(错误:用作表达式的子查询返回多于一行
)
数据库:PostgreSQL
最佳答案
如果我理解正确,那么您的查询过于复杂了。这是一个简单的聚合:
select productid, sum(qty) as totQty
from tmpprch
group by productid;
您从哪里得到查询形式的想法?
关于sql - 添加新列到现有表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23629735/