我有一个这样的表结构:
-----------------------------
|Branch |Cluster |Point |
-----------------------------
|branch a |Cluster 1 |1000 |
-----------------------------
|branch a |Cluster 2 |2000 |
-----------------------------
|branch b |Cluster 3 |1000 |
-----------------------------
|branch b |Cluster 4 |1000 |
-----------------------------
我想选择每个分支的总数。所以我的输出就像
-----------------------------
|Branch |Cluster |Point |
-----------------------------
|branch a |Cluster 1 |1000 |
-----------------------------
|branch a |Cluster 2 |2000 |
-----------------------------
|Total branch a |3000 |
-----------------------------
|branch b |Cluster 4 |1000 |
-----------------------------
|branch b |Cluster 3 |1000 |
-----------------------------
|Total branch b |2000 |
-----------------------------
一个简单的sql方法是使用union和order by(限制是order by需要正确的总计内容格式)
否则你应该格式化结果服务器端
select Branch, Cluster, Point
from my_table
union all
select concat(Branch, ' Total ' , null, sum(Point)
from my_table
order by Branch
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)