我有这个选择:
SELECT
m.`maschine-name` AS byMaschine,
q.`mname` AS byMName,
SUM(YEAR(q.`created`) = YEAR(CURDATE())) AS total
FROM qualitaet q INNER JOIN
maschinen m
ON m.maschine = q.maschine
WHERE
q.`status`='1'
GROUP BY
q.maschine, q.mname;
要获取当前年度的所有结果,它如下所示:
| maschine-name | mname | total |
| TYP 1 | 0 | 4 |
| TYP 2 | 3 | 4 |
| TYP 2 | 4 | 4 |
| TYP 3 | 0 | 4 |
| TYP 4 | 0 | 4 |
请参见SQL小提琴here
但我想选择它作为财政年度(财政年度),从>=10月1日开始,得到这个结果:
| maschine-name | mname | total |
| TYP 1 | 0 | 3 |
| TYP 2 | 3 | 2 |
| TYP 2 | 4 | 0 |
| TYP 3 | 0 | 2 |
| TYP 4 | 0 | 2 |
我有不同的日期报表,但会计年度让我抓狂:-(
显示今天的数据:
SUM(DATE(created) = CURDATE()) AS total
显示本周的数据:
SUM(YEARWEEK(q.`created`, 1) = YEARWEEK(CURRENT_DATE, 1)) AS total
显示当前月份的数据:
SUM(q.`created` >= CURDATE() - INTERVAL DAY(CURDATE())-1 DAY) AS total
显示当前年度的数据:
SUM(YEAR(q.`created`) = YEAR(CURDATE())) AS total
有没有办法从上面得到这个结果?
向您致以最良好的祝愿,祝您新年快乐
最佳答案
我是用MAKEDATE做的。开始日期是10月1日
SUM(q.`created` >= MAKEDATE(year(now()-interval 1 year),1) + interval 9 month) AS total
参见SQLFiddlehere
完成选择:
SELECT
m.`maschine-name` AS byMaschine,
q.`mname` AS byMName,
SUM(q.`created` >= MAKEDATE(year(now()-interval 1 year),1) + interval 9 month) AS total
FROM qualitaet q INNER JOIN
maschinen m
ON m.maschine = q.maschine
WHERE
q.`status`='1'
GROUP BY
q.maschine, q.mname;
现在我收到这个结果:
| maschine-name | mname | total |
| TYP 1 | 0 | 3 |
| TYP 2 | 3 | 2 |
| TYP 2 | 4 | 0 |
| TYP 3 | 0 | 2 |
| TYP 4 | 0 | 2 |
关于mysql - MySQL-选择作为会计年度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34552181/