0

I have a quick question when dealing with null values in a table. I'm trying to sum up all sales made by each sales person below:

Person Product Price
Bob Prod1 10.99
Sue Prod1 10.99
Bob Prod1
Anna Prod2 50.99

In this case, Bob didn't enter his price and left it null but it would be the same 10.99 value. How would I write a query that delivers total sales by sales person like below:

Person Product Price
Bob Prod1 21.98
Sue Prod1 10.99
Anna Prod2 50.99
2
  • I need the query to pull forward the last non-null value for Prod1 so that I can do: SELECT Person, SUM(Price) AS PriceSum FROM Table GROUP BY Person Commented Jan 14, 2022 at 18:39
  • 1
    Replace the nulls with the average price per product first. Then run that SQL command. Commented Jan 14, 2022 at 18:47

2 Answers 2

1

You could use max (or min or avg) as a windowed aggregate to replace the nulls per person & product:

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
Sign up to request clarification or add additional context in comments.

Comments

0

SELECT Person, SUM(Price) AS PriceSum FROM Table GROUP BY Person should give you the desired results since you're grouping by Person they would be aggregated under Bob

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.