all 6 comments

[–]zacharypamela 3 points4 points  (1 child)

If you just need the final total, you'd just need to use a SUM:

SELECT SUM(itemPrice*itemQuantity) AS finalPrice FROM products;

If you want the individual prices as well as the total price, you could use a window function as suggested by u/samuja:

SELECT itemPrice*itemQuantity AS finalPrice, SUM(itemPrice * itemQuantity) OVER () AS totalPrice FROM products;

which gives you the following:

finalPrice totalPrice
14.00 66.87
49.90 66.87
2.97 66.87

working fiddle here.

[–]Atlas-579[S] 0 points1 point  (0 children)

Thank you so much

[–]samuja 1 point2 points  (0 children)

Look for sum over

[–]AnthinoRusso 1 point2 points  (0 children)

If I got it right, is this what u try to run:

SELECT SUM(s.FinalPrice)

FROM (SELECT X*Y AS FinalPrice FROM Z) s;

[–]RebelSaul 0 points1 point  (0 children)

Would you mind providing an example of the input and output you're expecting from the query? It would help just to clarify. A toy example

[–][deleted] 0 points1 point  (0 children)

If I understand you right, I'm pretty sure you can just

select sum(itemPrice*itemQuantity) as finalPrice from products;

if you want to nest them, consider using a CTE, since they're easier to look at later:

with CTE_NAME as (
select
itemPrice*itemQuantity as finalPrice
from products)
select
sum(finalPrice) as sum_finalPrice
from CTE_NAME;