Please help me with the sum function

Database

Please help me with the sum function

I have a table called SALESAGGRT where i have two columns Product_Id , Total_Sales

in the table i have following data

Product_Id Total_Sales
100 1000
200 2000
300 3000
400 4000

I want in the separate column total sum of all the sales

for ex :-

Product_Id Total_Sales Sum(Total_Sales)
100 1000 10000
200 2000 10000
300 3000 10000
400 4000 10000

what should be the sql for generating the above result set.

Please help me.Thanks in advance.
Tags (1)
2 REPLIES
Enthusiast

Re: Please help me with the sum function

You can do a Group Sum using the SUM() OVER(PARTITION BY Product_ID) window aggregate. However, your example indicates that the Total_Sales Column is a Cumulative Sum. The derivation to Sum(Total_Sales) in your example is not clear without either a quantity sold field or the existence of a SalesDetail table.
Enthusiast

Re: Please help me with the sum function

Select Product_Id
, Total_Sales
, D1.AllSales
From tbl
Cross Join (Select Sum(Total_Sales) As AllSales
From tbl) D1