I want to create a column in SQL similar to the flag
below where I can identify the top 20th percent and bottom 20th percent of sales per block group in a given time period. I already have the sales aggregated to the block group but now I'm having trouble flagging them. For example, in bg2010 1
, there were 215 sales which are roughly the top 20th percent (actually 21.5 percent, but that's okay) of all sales within that time period.
I've tried percentile_cont
command and doesn't seem to be what I'm looking for, but that's also probably because I don't fully understand it so any help will be appreciated!
bg2010 sales16_17 flag
1 215 top 20th
2 150
3 115
4 100
5 95
6 95
7 85
8 65 bottom 20th
9 45 bottom 20th
10 35 bottom 20th