Get column sum and use to calculate percent of total (mySQL)

后端 未结 3 1381
眼角桃花
眼角桃花 2020-12-10 04:28

Here is a very basic look at my table. I have columns 1 and 2 and need to generate column 3. Column 3 is simply the total of the Number column for all Nam

3条回答
  •  甜味超标
    2020-12-10 04:58

    You just need to CROSS JOIN the SUM() of Number column:

    SELECT Name, Number, Number * 100 / t.s AS `% of total`
    FROM mytable
    CROSS JOIN (SELECT SUM(Number) AS s FROM mytable) t
    

    Demo Here

提交回复
热议问题