Skip to content
Advertisement

sum of value based on a category

I want to print the total sum of categories in every row using my-SQL and I am not able to do it can anyone please help me,

example:

store region sales
106 Atlanta 250000
107 Atlanta 300000
108 New England 100000
109 New England 150000
110 New England 270000

to convert it into a table like below

store region sales total sales
105 Atlanta 200000 750000
106 Atlanta 250000 750000
107 Atlanta 300000 750000
108 New England 100000 520000
109 New England 150000 520000
110 New England 270000 520000

Advertisement

Answer

You can use sum as a window function:

select *, 
Sum(sales) over (partition by region) as TotalSales
from table
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement