如何根据其ID添加返回的给定产品的所有qtys。
我尝试了什么。
PRODID, ITEMDES, QTY, SUM(QTY where PRODID = PRODID )
as total-qty-for-each-product,
答案 0 :(得分:4)
您将使用分析函数:
select
PRODID, ITEMDES, QTY,
SUM(QTY) over (partition by PRODID) as total-qty-for-each-product
from
products
;
这与普通SUM
的工作方式类似,区别在于它将所有值与内联的PRODID相加。结果与以下查询相同:
select
PRODID, ITEMDES, QTY,
(select SUM(QTY) from products p where p.PRODID = PRODID)
as total-qty-for-each-product
from
products
;
最好解释分析函数by the experts。