我正在尝试配置一个select,它从表中检索所有数据,但当前月份的数据除外。
select COUNT(INCIDENT_ID),
DATE_FORMAT(CREATE_DATE,'%m-%Y') AS formatted_date
from incident_view
where (create_year = YEAR(CURDATE()) or create_year = YEAR(CURDATE())-1)
AND CREATE_DATE NOT (create_month = MONTH(NOW())
AND CUSTOMER_COMPANY_NAME = "Company"
GROUP BY formatted_date
ORDER BY
YEAR(CREATE_DATE), MONTH(CREATE_DATE)
感谢任何帮助。
干杯
答案 0 :(得分:0)
您可以使用DATE_FORMAT功能格式化和比较月份(和年份)。更新的查询看起来像下面的那个(尽管还没有测试过它):
SELECT COUNT(INCIDENT_ID),
DATE_FORMAT(CREATE_DATE,'%m-%Y') AS formatted_date
from incident_view
where (create_year = YEAR(CURDATE()) or create_year = YEAR(CURDATE())-1)
AND DATE_FORMAT(CREATE_DATE ,'%Y%m') <> DATE_FORMAT(now() ,'%Y%m')
AND CUSTOMER_COMPANY_NAME = "Company"
GROUP BY formatted_date
ORDER BY
YEAR(CREATE_DATE), MONTH(CREATE_DATE)