0

我有一个存储发票数据的表。一旦它被支付is_paid将被标记为1

所以我要做的是获取(where is_paid =0)每个用户在给定日期的所有未付发票,以及每个员工的总未付金额和该期间的未付发票数量

SELECT
              receivable.amount,
              receivable.admission,
receivable.grouped,
              receivable.month,
              emp.name
            FROM
              receivable JOIN students ON receivable.emp_id = emp.id
where receivable.is_paid='0' 
group by receivable.emp_id
ORDER BY CAST(receivable.grouped AS UNSIGNED ) , receivable.grouped

如何更改上述代码以计算给定期间的待处理发票计数、每个用户的待处理金额总和以及该期间分组的总和(用户输入日期,因此需要在此之前显示所有记录,包括那个日期)

以下是报告结构

在此处输入图像描述

任何帮助将不胜感激

4

1 回答 1

0

猜测是month指本月的第一天——如果你能澄清这一点,我可以写出更准确的东西。本质上,您需要使用 SUM 和 COUNT 聚合函数,并声明要作为 GROUP BY 子句的一部分返回的所有其他列:

SELECT
  receivable.emp_id,
  emp.name,
  receivable.grouped,
  receivable.month,
  SUM(receivable.amount) as totalAmount
  COUNT(*) as totalCount
FROM receivable 
JOIN emp ON receivable.emp_id = emp.id
WHERE receivable.is_paid='0'
  AND receivable.month = '2013-01-01'
GROUP BY receivable.emp_id, emp.name, receivable.grouped, receivable.month
ORDER BY CAST(receivable.grouped AS UNSIGNED ), receivable.grouped
于 2013-01-16T17:05:52.750 回答