I have a payment table looks like below:

我的付款表如下所示:

+--------------+---------+
| Type         | Amount  |
+--------------+---------+
| General      | 500     |
| General      | 700     |
| Assets       | 450     |
| Business     | 270     |
| General      | 70      |
| Assets       | 350     |
+--------------+---------+

I want the sum and percentage group by type.

我希望按类型划分总和和百分比。

My query is --

我的疑问是 -

SELECT fld_type, SUM(fld_amount) FROM tbl_payment group by fld_type


+--------------+---------+
| Type         | Amount  |
+--------------+---------+
| General      | 1270    |
| Assets       | 800     |
| Business     | 270     | 
+--------------+---------+

I just getting the sum but can't calculate percentage by sum of amount in this case 2340, and calulate with each group item, such as for general -

我只是得到了总和,但是不能用这个案例中的金额总和来计算百分比2340,并且用每个组项目计算,例如一般 -

1270*100/2340 = 56.27

1270 * 100/2340 = 56.27

like this --

喜欢这个 -

+--------------+---------+----------+
| Type         | Amount  | Percent% |
+--------------+---------+----------+
| General      | 1270    |   54.27  |
| Assets       | 800     |   34.19  |
| Business     | 270     |   11.54  |
+--------------+---------+----------+

Any help is appreciated.

任何帮助表示赞赏。

2 个解决方案

#1


1

To get the percentage for each group you could do it like

要获得每个组的百分比,您可以这样做

select fld_type, 
sum(fld_amount),
(sum(fld_amount) / t.total) * 100 percent
from tbl_payment 
cross join (
select sum(fld_amount) total from tbl_payment
) t
group by fld_type

Demo

更多相关文章

  1. 没有得到Jquery / Javascript中的总和
  2. 求一条sql语句:计算两列的差值,以及各个差值占差值总和的比例

随机推荐

  1. 编译android源码时,jack server 出现out o
  2. linux目录结构详细介绍
  3. arm-linux学习笔记3-linux内存管理与文件
  4. 和菜鸟一起学linux之DBUS基础学习记录
  5. 修复linux grub引导程序
  6. 正则将长数字转为英式写法(从后向前3个数
  7. Linux实用程序编写了一个matlab .mat文件
  8. 【精】Linux磁盘I/O性能监控之iostat详解
  9. Linux入门笔记之一:系统分区及挂载点
  10. ldconfig报错 :libstdc++.so.6.0.18-gdb.p