SQL Dealing with NULL Values



在处理表中的空值时,我有一个快速问题。我试图把每个销售人员的所有销售额总结如下:

价格
人员 产品
Bob
起诉 Prod1 10.99
Bob
Anna

您可以使用max(或min或avg(作为窗口聚合来替换每个人的null;产品:

Select Person, Product, sum(Price) Price
from (
select person, product, 
Coalesce(price, Max(price) over(partition by person, product)) Price
from t
)t
group by person, product

SELECT Person, SUM(Price) AS PriceSum FROM Table GROUP BY Person应该会为您提供所需的结果,因为您是按人员分组的,它们将在Bob下聚合