在MySQL中按组计算合计
我有一个小php程序,用于显示每个工作人员的所有待定金额,但我在合计acc_代码的组值时遇到了一些问题在MySQL中按组计算合计,mysql,sql,Mysql,Sql,我有一个小php程序,用于显示每个工作人员的所有待定金额,但我在合计acc_代码的组值时遇到了一些问题 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, (SELEC
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,
(SELECT SUM(pending.amount) FROM pending p join staff s on p.admission_numb = s.admission WHERE p.acc_code = staff.acc_code) acc_code_total_pending
from
pending join staff
on pending.admission_numb = staff.admission
group by
admission
order by
staff.acc_code asc
我已经在下面解释了这个系统。为每位员工分配一个acc_代码。每个账户代码有40-50名员工
例如:
预期产出:
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_代码的总待定金额
下面是我编写的代码,但我不确定如何获得每个ac_代码的总计
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
如有任何帮助,我们将不胜感激。您需要按acc_代码分组并计算月数。大概是这样的:
select SUM ((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 staff.acc_code order by staff.acc_code asc
请注意,我没有查看您的查询。我只是补充了我认为你缺少的东西。
当您试图为团队获取结果时,您需要员工的姓名做什么?这里有一种获取acc_代码总数的方法
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
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,
(SELECT SUM(pending.amount) FROM pending p join staff s on p.admission_numb = s.admission WHERE p.acc_code = staff.acc_code) acc_code_total_pending
from
pending join staff
on pending.admission_numb = staff.admission
group by
admission
order by
staff.acc_code asc
我需要显示每个acc_代码的个人记录,我在我的代码中所做的,我只需要每个acc_代码的每个总数。分组bt acc_代码不起作用。我需要计算每个会计年度(而不是每个月)的待定金额。为什么不使用预期结果表(按照您希望的显示方式,手动编写)编辑您的问题,因为我无法理解您真正需要的内容。我已添加了预期输出。感谢您不能在一个mysql结果集中有不同的行,因此预期的结果是不可能的。相反,您可以有如下内容:
001 test1 3 10A 1000
<代码>002测试2 5 10A 1000等。我相信@Tom的最后一个答案可以实现这一点。这是正确的方法。如果它仍然不能产生一些结果,问题在于查询中的一些小细节,除非我们有数据库,否则我们无法解决这些问题。grouping by staff.acc_代码不会显示单个记录。需要单个记录(如我的代码)和每个acc_代码的总值