I have aggregated column one column in the table as following
aggregated table:
*-------------------*
| territory | users |
*-------------------*
| worldwide | 20 |
| usa | 6 |
| germany | 3 |
| australia | 2 |
| india | 5 |
| japan | 4 |
*-------------------*
Below is my desired result in which I want to replicate ‘worldwide’ data as new column so I can calculate distribution of users.
desired output:
*-----------------------------------------------------*
| territory | users | ww_user | % users distribution |
*-----------------------------------------------------*
| worldwide | 20 | 20 | 0.0 |
| usa | 6 | 20 | 0.30 |
| germany | 3 | 20 | 0.15 |
| australia | 2 | 20 | 0.10 |
| india | 5 | 20 | 0.25 |
| japan | 4 | 20 | 0.20 |
*-----------------------------------------------------*
I have tried few options in db<>fiddle but none of them are getting me desired results.
can anyone give me a direction/idea on how to get my desired results?
2
Answers
You can add a CASE WHEN to replace the 1 in worldwide
fiddle
There are a number of ways to do this but it seems like fixing your fiddle would be easiest. https://dbfiddle.uk/YZLCxDxO
You had a typo (ww vs worldwide) and you need to use the SUM() window function. Otherwise you were on the right track.