1

Let's say I have a table with two columns:

  1. Month
  2. Value

I now want to create a 3rd column that will aggregate that value for each month (and will display the total product for each month in each row that is part of this month. Any general function that could do it without me having to specify each case in the function? (If I were to use an "If" function, that's what I would do..)

Example Given: My initial 2 columns are "Month" and "Value" and I want a function that will create the "Sum" comlumn.

Month  Value     Sum
6      23        57
6      34        57
7      56        100
7      44        100
8      12        12
Valentino
  • 7,291
  • 6
  • 18
  • 34
  • Maybe could you add a sample of this table and the desired output to clarify your question. And also what you have tried so far. You tagged `group-by`, I guess that is the general function you want. – Valentino May 19 '19 at 16:54
  • @Valentino Thanks for the comment. I actually didn't try anything yet, as I have no idea how to approach it. Can I attach a table/picture to a comment in here? – tom schneorson May 20 '19 at 17:22
  • Not in the comment, but you can [edit](https://stackoverflow.com/posts/56209247/edit) your question. Better a text table than an image. You can format it by selecting the text and click on `{}` in the bar on top of the textarea where you write. – Valentino May 20 '19 at 17:58
  • @Valentino Thanks, I have now changed it and added a table. Any thoughts? – tom schneorson May 21 '19 at 12:33

1 Answers1

0

General procedure

The general procedure would be to use sum with group by 'Month'. This will return a table where Month is used as index (no repeated months) and you have a column with the sums of all the months.

Using to your example, you will get the following intermediate table:

Month  Sum
6      57
7      100
8      12

Then you can use this table to create the Sum column in your original table.
Basically you just copy the value in Sum from the intermediate table in all the row with the same Month.

Now how to do this depends on the technology you use, which you have not specified, so I make a couple of examples with the tools I know: if you use different tools, try to adapt the idea.

Using python pandas

Suppose that df is your original table (without the Sum column) created with pandas. Then the procedure would be:

#create the intermediate table using sum and groupby
idf = df.groupby('Month').sum()

#edit idf so that the number of rows is the same of df
idf = idf.loc[df['Month']].reset_index()

#adding the column to the original table
df['Sum'] = idf['Value']

Using MySQL

Suppose that otable is your original table (without the Sum column) in a MySQL database. Then the procedure would be:

#create additional column to store the Sum, null by default 
ALTER TABLE otable ADD COLUMN Sum INT;

#create the intermediate table using sum and groupby
CREATE TEMPORARY TABLE idf SELECT SUM(Value) FROM otable GROUP BY Month;

#update the original table
UPDATE otable o, idf i SET o.Sum = i.`SUM(Value)` WHERE o.Month = i.Month;

Using excel

For excel it has been already answered here and here.

Valentino
  • 7,291
  • 6
  • 18
  • 34
  • Thanks, Valentino! My initial intention was to use Excel. I also use MySQL, so that is very helpful. I will try to do it and will write here after I do. Still, any thoughts about doing it in Excel? – tom schneorson May 21 '19 at 17:01
  • I'm not an excel expert, let me see if I can figure how to do it quickly – Valentino May 21 '19 at 17:03