mysql - MySQL中按组计算总计

标签 mysql sql

我有一个小型 php 程序,用于显示每个员工的所有待处理金额,但我在合计 acc_code 的组值时遇到一些问题。

我已经解释了下面的系统。每个员工都分配有一个 acc_code。每个帐户代码有 40 – 50 名工作人员

例如:

admission  name   months  acc_code
==================================
001        test1  3       10A
002        test2  5       10A
006        test3  7       15B
008        test4  1       15A
011        test5  2       16C
051        test6  3       16A
012        test7  3       16A

预期输出:

 admission  name   months  acc_code
    ==================================
    001        test1  3       10A
    002        test2  5       10A
                    Total    USD 1000

    006        test3  7       15B
                    Total    USD 1800

    008        test4  1       15A
                    Total    USD 800

    011        test5  2       16C
                    Total    USD 1600

    051        test6  3       16A
    012        test7  3       16A
                     Total    USD 2700

每个员工都有一定的分配金额。我需要获取每个 acc_code 的总待处理金额

下面是我编写的代码,但我不确定如何获取每个 ac_code 的总计

select
  (period_diff(date_format(now(), '%Y%m'),
  date_format(month, '%Y%m'))) as months,
  pending.amount,
  pending.admission_numb,
  pending.month,
  staff.full_name,
  staff.acc_code
from
  pending join staff 
  on pending.admission_numb = staff.admission 
group by
  admission
order by
  staff.acc_code asc

任何帮助将不胜感激

最佳答案

select
  staff.acc_code,
  SUM(pending.amount) pending_amount
from
  pending join staff 
  on pending.admission_numb = staff.admission 
group by
  staff.acc_code
order by
  staff.acc_code asc

关于mysql - MySQL中按组计算总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13419520/

相关文章:

c# - 何时在 Entity Framework Code First 中使用存储过程和 SQL 查询

mysql - SQL查询从内部连接返回多个数据

java - 打破 @Query 的 LIMIT

mysql - 如何使用第三个表连接两个没有公共(public)列的 SQL 表,第三个表包含这两个表的列

sql - 如何将扩展加载到 SQLite 中?

java - 将 jdbc 连接到 openshift 上的 MySql 服务器时出现问题

javascript - 使用 Javascript 倒数计时器和 PHP 变量时出现问题

php - 如何检查字符串是否被转义两次

php - 如何在 Fullcalendar 中保存事件?

php - MySQL - 如果由 CLI 调用存储过程返回 null,但会导致工作台