'How to get percentage of one column based off different totals in PostgreSQL
POSTGRESQL: I have a table of three columns and 2000+ rows. The columns are (clothing) item, size, and total(price). I need to calculate a percentage column that reflects the fraction of the total price that should be allocated to each item and size combo.
table example
| item | size | price |
| ---- | ---- | ----- |
|shirt | M | 3.99 |
|pants | S | 2.99 |
|shirt | S | 2.50 |
|shirt | L | 4.25 |
|pants | S | 4.30 |
|shirt | S | 6.50 |
|shirt | M | 2.99 |
|shirt | L | 1.25 |
What I want:
| item | size | price | percentage |
| ---- | ---- | ----- | ---------- |
|shirt | M | 3.99 |57.16 |
|pants | S | 2.99 |41.02 |
|shirt | S | 2.50 |27.78 |
|shirt | L | 4.25 |77.27 |
|pants | S | 4.30 |58.98 |
|shirt | S | 6.50 |72.22 |
|shirt | M | 2.99 |42.83 |
|shirt | L | 1.25 |22.72 |
I don't even know where to start... Thanks
**The table's looked normal in preview but now are looking quite odd. I hope you can still understand
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
