I’m not sure how to ask this question in a way that Google will understand. I want to left join two tables and have the blank fields filled in. Here’s what I’m working with:
tbl1
| user | date | usage |
| us1 | 01/12 | 503 |
| us1 | 02/12 | 245 |
| us1 | 03/12 | 465 |
| us2 | 02/12 | 327 |
| us2 | 03/12 | 204 |
| us3 | 02/12 | 156 |
tbl2
| dates | avg_use |
| 01/12 | 345 |
| 02/12 | 426 |
| 03/12 | 502 |
Desired output
| user | date | usage | pct_of_avg |
| us1 | 01/12 | 503 | 1.45 |
| us1 | 02/12 | 245 | .58 |
| us1 | 03/12 | 465 | .93 |
| us2 | 01/12 | (null) | 0 |
| us2 | 02/12 | 327 | .95 |
| us2 | 03/12 | 204 | .41 |
| us3 | 01/12 | (null) | 0 |
| us3 | 02/12 | 156 | .37 |
| us3 | 03/12 | (null) | 0 |
I understand JOINs pretty well, so I’m aware that a typical JOIN will not be able to do this since the data in tbl1.user won’t exist. Is there a way to do this in SQL at all? Bonus points for giving what I’m trying to do a name, because it would help a lot just to know how to Google this 🙂
This should do it: