【发布时间】:2019-06-21 11:36:44
【问题描述】:
我的表 myTable 如下所示:
+-------+-------+--------+------------+
| Id | Agent | Qualif | Date |
+-------+-------+--------+------------+
| 1 | A | Q1 | 2019-05-11 |
| 2 | B | Q2 | 2019-05-11 |
| 3 | C | Q1 | 2019-05-12 |
| 4 | A | Q1 | 2019-05-11 |
| 5 | A | Q2 | 2019-05-11 |
+-------+-------+--------+------------+
对于给定的日期范围,我需要显示每个日期、每个代理和 count(rows)-count(qualif = Q1) / count(rows) 的行数
为了解释更多,我在我的 PHP 页面上做的方式是先查询:
SELECT date, Agent, count(*) as tot
FROM myTable
WHERE date between d1 and d2
group by date, agent
当遍历每个代理和日期的结果时,我查询:
SELECT count(*) as totQ1 FROM my table WHERE date='somedate' and agent='someagent' and qualif='Q1'
“2019-05-11”和“2019-05-12”之间的预期结果
+------------+--------+-----+-----------+
| date | Agent | tot | division |
+------------+--------+-----+-----------+
| 2019-05-11 | A | 3 | 2/3 |
| 2019-05-11 | B | 1 | 1 |
| 2019-05-12 | C | 1 | 1 |
+------------+--------+-----+-----------+
考虑 tot=0;
有什么方法可以将所有这些合并到一个查询中,这样我就不必为每个日期和代理查询多次?
【问题讨论】:
-
请添加特定日期范围的预期结果。