Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating sum within groups on multiple variable

I have a table in Microsoft SQL Server with municipality IDs, company IDs and number of employees and I want to get the sum of employees per municipality and also the sum of employees per company Id and per municipality. So the table currently looks like this

| Municipality | Company Type ID | Number of Employees | Total employees in same municipality | Total Employees in same economic sector and same Municipality |
|--------------|-----------------|---------------------|--------------------------------------|---------------------------------------------------------------|
| 03212        | 10332           | 0                   |                                      |                                                               |
| 03212        | 10332           | 12                  |                                      |                                                               |
| 03212        | 10332           | 2                   |                                      |                                                               |
| 03212        | 10332           | 3                   |                                      |                                                               |
| 03212        | 10333           | 4                   |                                      |                                                               |
| 03212        | 10333           | 1                   |                                      |                                                               |
| 03133        | 10554           | 4                   |                                      |                                                               |
| 03133        | 10554           | 55                  |                                      |                                                               |
| 03133        | 10554           | 4                   |                                      |                                                               |

But I want to achieve something like this

| Municipality | Company Type ID | Number of Employees | Total employees in same municipality | Total Employees in same company ID and same Municipality |
|--------------|-----------------|---------------------|--------------------------------------|----------------------------------------------------------|
| 03212        | 10332           | 0                   | 22                                   | 17                                                       |
| 03212        | 10332           | 12                  | 22                                   | 17                                                       |
| 03212        | 10332           | 2                   | 22                                   | 17                                                       |
| 03212        | 10332           | 3                   | 22                                   | 17                                                       |
| 03212        | 10333           | 4                   | 22                                   | 5                                                        |
| 03212        | 10333           | 1                   | 22                                   | 5                                                        |
| 03133        | 10554           | 4                   | 63                                   | 59                                                       |
| 03133        | 10554           | 55                  | 63                                   | 59                                                       |
| 03133        | 10555           | 4                   | 63                                   | 4                                                        |

I have tried using the SUM OVER PARTITION but that only allows me to do it for one variable. How can I create the sum and insert it into the table over multiple variables

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Sum(no of employees) over (partition by company type, Municipality) as 'Total Employees by Municipality

Something like that. It’s just another column like you did previously .

I’m on mobile so formatting isn’t correct .

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading