本文介绍了MySQL中的动态交叉表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表,其中包含以下数据

I have the following table with the following data



Year | Age Group | Male | Female
2000 | 0 - 25    | 50   | 100
2000 | 26 above  | 40   | 75
2001 | 0 - 25    | 150  | 86
2001 | 26 above  | 65   | 83

我想以以下格式创建交叉表

I would like to create a cross tab in the following format


                |  Male   | Female
2000            |  90     | 175
       0 - 25   |  50     | 100
       26 above |  40     | 75
2001            |  215    | 169
       0 - 25   |  150    | 86
       26 above |  65     | 83

非常感谢您的协助.

推荐答案

SQLFiddle示例 :

select * from
(
select year,ageGroup,male,female from t
union all
select year,'' ageGroup,sum(male) male,sum(female) female
from t group by year
) st
order by year,agegroup

这篇关于MySQL中的动态交叉表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 08:21