In Access:
SELECT Table2.category_ID AS category, count(*) AS incidents, sum(Table2.dead) AS dead, avg(Table2.dead) AS [avg]
FROM Table2
GROUP BY category_ID;
returns a table with the category table correctly looked up:
Conservative. Idaho. Software engineer. Historian. Trying to prevent Idiocracy from becoming a documentary.
Email complaints/requests about copyright infringement to clayton @ claytoncramer.com. Reminder: the last copyright troll that bothered me went bankrupt.
In Access:
SELECT Table2.category_ID AS category, count(*) AS incidents, sum(Table2.dead) AS dead, avg(Table2.dead) AS [avg]
FROM Table2
GROUP BY category_ID;
returns a table with the category table correctly looked up:
The query:
SELECT int(year/10)*10 AS decade, count(*) AS incidents, sum(incidents.dead) AS dead
FROM incidents
WHERE ([cause]='MI' or [cause]='MI?')
GROUP BY (int (year/10)*10);
This produces three columns:
| decade | incidents | dead |
|---|---|---|
| 1660 | 1 | 2 |
| 1800 | 1 | 8 |
| 1860 | 7 | 27 |
| 1870 | 4 | 19 |
| 1880 | 12 | 55 |
| 1890 | 19 | 78 |
| 1900 | 43 | 183 |
| 1910 | 21 | 83 |
| 1920 | 7 | 31 |
| 1940 | 2 | 18 |
| 1960 | 2 | 21 |
| 1970 | 6 | 27 |
| 1980 | 8 | 45 |
| 1990 | 9 | 48 |
| 2000 | 4 | 55 |
| 2010 | 11 | 170 |
| 2020 | 1 | 5 |
You will notice that there are no rows for the decades 1810 through 1859. This is because there are no incidents where the cause was clearly or likely mental illness. (If this seems odd, schizophrenia rates in Britain roughly octupled from the17th to 19th centuries, and similar dramatic increases happen in America over this same period. One possible cause is cats going from mousekillers in barns into homes as pets with toxoplasmosis in cat feces, as one possible environmental trigger.) So how do I get rows with 0 incidents into that query?
I tried:
SELECT int(year/10)*10 AS decade, count(*) AS incidents, sum(Table2.dead) AS 'dead'but the complaint is that "Your query does not include the specified expression int(year/10)*10 as an aggregate function."
FROM Table2
WHERE ((cause_ID=15) or (cause_ID=16))
UNION
SELECT int(year/10)*10 AS decade, count(*) AS incidents, sum(Table2.dead) AS 'dead'
FROM Table2
WHERE ((cause_ID<>15) and (cause_ID<>16))
GROUP BY (int (year/10)*10) ;
SELECT int(year/10)*10 AS decade, count(*) AS incidents, sum(Table2.dead) AS 'dead'
FROM Table2
WHERE ((cause_ID=15) or (cause_ID=16))
GROUP BY (int (year/10)*10)
UNION SELECT int(year/10)*10 AS decade, 0, 0
FROM Table2
WHERE ((cause_ID<>15) and (cause_ID<>16))
GROUP BY (int (year/10)*10);
However, if the first SELECT has matches, the UNION gives me two rows before DECADE, and some decades are still missing:
| decade | incidents | 'dead' |
|---|---|---|
| 1650 | 0 | 0 |
| 1660 | 1 | 2 |
| 1680 | 0 | 0 |
| 1690 | 0 | 0 |
| 1720 | 0 | 0 |
| 1750 | 0 | 0 |
| 1780 | 0 | 0 |
| 1800 | 0 | 0 |
| 1800 | 1 | 8 |
| 1820 | 0 | 0 |
| 1830 | 0 | 0 |
| 1840 | 0 | 0 |
| 1850 | 0 | 0 |
| 1860 | 0 | 0 |
| 1860 | 7 | 27 |
| 1870 | 0 | 0 |
| 1870 | 4 | 19 |
| 1880 | 0 | 0 |
| 1880 | 12 | 55 |
| 1890 | 0 | 0 |
| 1890 | 19 | 78 |
| 1900 | 0 | 0 |
| 1900 | 43 | 183 |
| 1910 | 0 | 0 |
| 1910 | 21 | 83 |
| 1920 | 0 | 0 |
| 1920 | 7 | 31 |
| 1930 | 0 | 0 |
| 1940 | 2 | 18 |
| 1950 | 0 | 0 |
| 1960 | 0 | 0 |
| 1960 | 2 | 21 |
| 1970 | 0 | 0 |
| 1970 | 6 | 27 |
| 1980 | 0 | 0 |
| 1980 | 8 | 45 |
| 1990 | 0 | 0 |
| 1990 | 9 | 48 |
| 2000 | 0 | 0 |
| 2000 | 4 | 55 |
| 2010 | 0 | 0 |
| 2010 | 11 | 170 |
| 2020 | 0 | 0 |
| 2020 | 1 | 5 |
I think instead of a UNION I need to only do the second SELECT if there is no record from the first SELECT.
| 'dead' | 'Category' |
|---|---|
| 671 | FAM |
| 357 | FAMNONRES |
| 15 | PRIV |
| 5970 | PUB |
| 169 | RES |
| 124 | SCHOOL |
| 18 | UNKNOWN |
| 165 | WORK |
| 12 | WORSHIP |
| SumOfdead | cause | |
| 6 | 4 | |
| 3 | 5 | |
| 2 | 8 | |
| 3 | 10 | |
| 11 | 18 | |
| 3 | 20 | |
| 3 | 24 | |