doujiu3768 2016-11-06 10:42
浏览 90

在mysql中多次求和

I have a table structure like this:

-----------------------------
|Branch   |Cluster   |Point |
-----------------------------
|branch a |Cluster 1 |1000  |
-----------------------------
|branch a |Cluster 2 |2000  |
-----------------------------
|branch b |Cluster 3 |1000  |
-----------------------------
|branch b |Cluster 4 |1000  |
-----------------------------

I would like to select the total of each branch. So my output would be like

-----------------------------
|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  |
-----------------------------
  • 写回答

4条回答 默认 最新

  • duanjianfu1398 2016-11-06 10:48
    关注
    (
        SELECT Branch, Cluster, Point
        FROM yourTable
    )
    UNION ALL
    (
        SELECT Branch, 'Total' AS Cluster, SUM(Point) AS Point
        FROM yourTable
        GROUP BY Branch
    )
    ORDER BY Branch, Cluster
    

    This assumes that all the data in the Cluster column really begins with the letter 'C'.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号