So I have this table:
I am trying to produce a column that will look like this (containing the computed results)
I tried this query by it gives an error
SELECT Name, SUM(Amount) AS TAmnt,
ProductPrice-SUM(Amount) AS OutB
FROM t1
GROUP BY Name;
I tried this as well, but the results don’t seem to be what i want
SELECT Name,SUM(Amount) AS TAmnt,
SUM(ProductPrice)-SUM(Amount) AS OutB
FROM t1
GROUP BY Name;
Any help to see what I am missing?
2
Answers
You may try aggregating by name and product price:
As mentioned by @Barmar, your current table is not normalized. Given that a product has only one price, you should create a separate prices table, and store this information there.
Use
ANY_VALUE()
to prevent the error. It will select theProductPrice
value from any row in the group.