我有一个简单的查询:
SELECT sds.district_id,detail.year, detail.race, SUM(count)
FROM school_data_race_ethnicity_raw as detail
INNER JOIN school_data_schools as sds USING (school_id)
GROUP BY district_id, year, race
样本结果集:
| 68080104 | 2009 | Multiracial | 0 |
| 68080104 | 2009 | White | 847 |
| 68080104 | 2010 | American Indian | 1 |
| 68080104 | 2010 | Asian | 4 |
| 68080104 | 2010 | Black | 17 |
| 68080104 | 2010 | Hispanic | 4 |
| 68080104 | 2010 | Multiracial | 2 |
| 68080104 | 2010 | White | 823 |
| 68080104 | 2011 | American Indian | 4 |
| 68080104 | 2011 | Asian | 4 |
| 68080104 | 2011 | Black | 9 |
| 68080104 | 2011 | Hispanic | 10 |
| 68080104 | 2011 | Multiracial | 24 |
| 68080104 | 2011 | White | 767 |
+-------------+------+-----------------+------------+
我想添加一个名为 total 的第 5 列,它显示给定年份和地区总人口的总和。例如,如果我在 2011 年在 68080104 区,那么总数将是 (4+4+9+10+24+767)。我需要它作为此查询中的另一列。它还需要快速。(不到 10 秒)。我正在努力解决如何做到这一点而不影响速度和数据。