简体   繁体   English

MySQL-选择作为会计年度

[英]MySQL - select as fiscal year

I have this SELECT: 我有这个选择:

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;

to get all results for the current year and it looks like this: 获取当年的所有结果,它看起来像这样:

| maschine-name | mname | total |
|     TYP 1     |   0   |   4   |
|     TYP 2     |   3   |   4   |
|     TYP 2     |   4   |   4   |
|     TYP 3     |   0   |   4   |
|     TYP 4     |   0   |   4   |

see SQL Fiddle here 在这里查看SQL Fiddle

But i want to SELECT it as fiscal year (financial year) starting at >= Oct, 1 to get this result: 但我想将其选择为从> = 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   |

i have different Date statements which work all, but the fiscal year drives me crazy :-( 我有不同的Date语句,它们都能正常工作,但是会计年度让我发疯了:-(

show data for TODAY : 显示今天的数据:

SUM(DATE(created) = CURDATE()) AS total

show data for CURRENT WEEK : 显示CURRENT WEEK的数据:

SUM(YEARWEEK(q.`created`, 1) = YEARWEEK(CURRENT_DATE, 1)) AS total

show data for CURRENT MONTH : 显示CURRENT MONTH的数据:

SUM(q.`created` >= CURDATE() - INTERVAL DAY(CURDATE())-1 DAY) AS total

show data for CURRENT YEAR : 显示CURRENT YEAR的数据:

SUM(YEAR(q.`created`) = YEAR(CURDATE())) AS total


Is there a way to get this result from above? 有没有办法从上面得到这个结果?

Best regards and a happy new year ;-) 最好的问候和新年快乐;-)

I did it with MAKEDATE. 我是在MAKEDATE完成的。 Startdate is Oct, 1 开始日期为1月10日

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

see SQLFiddle here 在这里看到SQLFiddle

complete SELECT: 完成选择:

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;

Now i receive this result: 现在我收到此结果:

| maschine-name | mname | total |
|     TYP 1     |   0   |   3   |
|     TYP 2     |   3   |   2   |
|     TYP 2     |   4   |   0   |
|     TYP 3     |   0   |   2   |
|     TYP 4     |   0   |   2   |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM