1

我有这个选择:

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 Fiddle

但我想选择它作为财政年度(财政年度)从 >= Oct, 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


有没有办法从上面得到这个结果?

最好的问候和新年快乐;-)

4

1 回答 1

2

我用 MAKEDATE 做到了。开始日期是 10 月 1 日

SUM(q.`created` >= MAKEDATE(year(now()-interval 1 year),1) + interval 9 month) AS total

在此处查看 SQLFiddle

完成选择:

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   |
于 2016-01-01T00:50:14.690 回答