上次你教我使用SELECT CASE进行旋转,这非常有帮助。 这次我需要更先进的帮助。
情况如下:
我有同样的旧死亡记录数据库;列存储原因包含所有原因的值,我使用select case将其转换为n列n个原因,如下所示
SELECT anno, cod_comune,
SUM(CASE sex WHEN 'M' THEN 1 ELSE 0 END) AS M,
SUM(CASE sex WHEN 'F' THEN 1 ELSE 0 END) AS F,
SUM(CASE cod_tit WHEN 'I' THEN 1 ELSE 0 END) AS dis_I,
SUM(CASE cod_tit WHEN 'II' THEN 1 ELSE 0 END) AS dis_II ,
SUM(CASE cod_tit WHEN 'III' THEN 1 ELSE 0 END) AS dis_III,
...
FROM casi
WHERE cod_comune>40000 AND cod_comune<200000 AND cod_comune NOT LIKE '%00'
GROUP BY anno, cod_comune, sex
ORDER BY anno, cod_comune, age;
通过这样做,我获得了每个城市的2行,一个用于男性,一个用于女性,以及每个原因导致的死亡人数。
也就是说,我得到这样的东西
+------+------------+------+------+-------+--------+---------+
| anno | cod_comune | M | F | dis_I | dis_II | dis_III |
+------+------------+------+------+-------+--------+---------+
| 2003 | 41001 | 0 | 19 | 0 | 5 | 0 |
| 2003 | 41001 | 20 | 0 | 0 | 7 | 0 |
| 2003 | 41002 | 12 | 0 | 0 | 3 | 0 |
| 2003 | 41002 | 0 | 16 | 0 | 6 | 0 |
| 2003 | 41003 | 8 | 0 | 0 | 1 | 0 |
| 2003 | 41003 | 0 | 6 | 0 | 2 | 0 |
| ... | ... | ... | ... | ... | ... | ... |
相反,我的教授说,如果我每个城镇只有一排,保持性别差异,那将是可取的。
所以,我试图将与原因相关的列加倍,例如causeI_m和causeI_f。我已经给出了两个条件来选择案例,但它不起作用,原因是我全部为零。
SELECT anno, cod_comune,
SUM(CASE sex WHEN 'M' THEN 1 ELSE 0 END) AS M,
SUM(CASE sex WHEN 'F' THEN 1 ELSE 0 END) AS F,
SUM(CASE sex WHEN 'M' AND cod_tit='I' THEN 1 ELSE 0 END) AS dis_Im,
SUM(CASE sex WHEN 'M' AND cod_tit='II' THEN 1 ELSE 0 END) AS dis_IIm ,
SUM(CASE sex WHEN 'M' AND cod_tit='III' THEN 1 ELSE 0 END) AS dis_IIIm,
...
SUM(CASE sex WHEN 'F' AND cod_tit='I' THEN 1 ELSE 0 END) AS dis_If,
SUM(CASE sex WHEN 'F' AND cod_tit='II' THEN 1 ELSE 0 END) AS dis_IIf ,
SUM(CASE sex WHEN 'F' AND cod_tit='III' THEN 1 ELSE 0 END) AS dis_IIIf,
...
FROM casi
WHERE cod_comune>40000 AND cod_comune<200000 AND cod_comune NOT LIKE '%00'
GROUP BY anno, cod_comune, sex
ORDER BY anno, cod_comune, age;
我从这个查询中得到了
+------+------------+------+------+--------+---------+--------+---------+
| anno | cod_comune | M | F | dis_Im | dis_IIm | dis_If | dis_IIf |
+------+------------+------+------+--------+---------+--------+---------+
| 2003 | 41001 | 20 | 19 | 0 | 0 | 0 | 0 |
| 2003 | 41002 | 12 | 16 | 0 | 0 | 0 | 0 |
| 2003 | 41003 | 8 | 6 | 0 | 0 | 0 | 0 |
| 2003 | 41004 | 7 | 5 | 0 | 0 | 0 | 0 |
| 2003 | 41005 | 2 | 5 | 0 | 0 | 0 | 0 |
| ... | ... | ... | ... | ... | ... | ... | ... |
我已经考虑过我已经命令数据库将性别等于该模态的情况加起来并且原因是我指定的那个。但有些事情是错的,我无法弄清楚是什么。 如果你给我一点帮助,我会很高兴的:) (我希望能很好地解释这个问题,英语仍然是我的第二语言)
答案 0 :(得分:1)
我可能错了,但我认为你应该删除&#34; sex&#34;分组声明中的列:
SELECT anno, cod_comune,
SUM(CASE sex WHEN 'M' THEN 1 ELSE 0 END) AS M,
SUM(CASE sex WHEN 'F' THEN 1 ELSE 0 END) AS F,
SUM(CASE WHEN sex='M' AND cod_tit='I' THEN 1 ELSE 0 END) AS dis_M_I,
SUM(CASE WHEN sex='M' AND cod_tit='II' THEN 1 ELSE 0 END) AS dis_M_II ,
SUM(CASE WHEN sex='M' AND cod_tit='III' THEN 1 ELSE 0 END) AS dis_M_III,
SUM(CASE WHEN sex='F' AND cod_tit='I' THEN 1 ELSE 0 END) AS dis_F_I,
SUM(CASE WHEN sex='F' AND cod_tit='II' THEN 1 ELSE 0 END) AS dis_F_II ,
SUM(CASE WHEN sex='F' AND cod_tit='III' THEN 1 ELSE 0 END) AS dis_F_III,
...
FROM casi
WHERE cod_comune>40000 AND cod_comune<200000 AND cod_comune NOT LIKE '%00'
GROUP BY anno, cod_comune -- !!! No sex here !!!
ORDER BY anno, cod_comune, age;
这将停止按行分割你的行。