I have a Google Big Query table called TableA that has ~3M records. There is a column called DimA (Dimension A) that has 20 values - 1 to 20. The counts by each value of DimA is shown in the summary table below in the Total column. I did some analysis and determined how much random sample I should draw from each value of DimA and it is shown in the column Sample. The % of sample drawn by each value of DimA is shown in column DimA_value_perc. I know how to do sample via brute force using the code below the table. However, this code is not scalable as the number of values of DimA grows and in case there are additional dimensions. Is there a more efficient way to do the stratified sampling? Thanks.
| DimA | Total | Sample | DimA_value_perc |
|---|---|---|---|
| 1 | 115,623 | 3,077 | 3% |
| 2 | 108,203 | 3,943 | 4% |
| 3 | 153,477 | 6,802 | 4% |
| 4 | 232,252 | 12,426 | 5% |
| 5 | 223,004 | 14,052 | 6% |
| 6 | 242,386 | 17,589 | 7% |
| 7 | 121,519 | 9,783 | 8% |
| 8 | 371,342 | 34,026 | 9% |
| 9 | 147,683 | 15,400 | 10% |
| 10 | 281,101 | 32,775 | 12% |
| 11 | 93,380 | 12,075 | 13% |
| 12 | 181,293 | 25,675 | 14% |
| 13 | 122,206 | 19,344 | 16% |
| 14 | 140,559 | 25,141 | 18% |
| 15 | 95,576 | 19,498 | 20% |
| 16 | 94,319 | 21,969 | 23% |
| 17 | 108,282 | 30,054 | 28% |
| 18 | 94,920 | 33,228 | 35% |
| 19 | 82,764 | 39,700 | 48% |
| 20 | 28,417 | 23,442 | 82% |
| Grand Total | 3,038,306 | 400,000 |
SELECT *
FROM tableA
where DimA = 1
order by rand()
limit 3077
union all
SELECT *
FROM tableA
where DimA = 2
order by rand()
limit 3943
etc