我如何在MySQL中获得四分之一的首次约会? [英] How do I get the first date of a quarter in MySQL?

查看:33
本文介绍了我如何在MySQL中获得四分之一的首次约会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我的代码如下:我试图找出要代替我的?的位置,以查找该日期所在的季度的开始日期.

The code that I have so far is below: I'm trying to find out what to put in place of my ?'s to find the start date of the quarter the date lies in.

SELECT
 QUARTER(r.callDate) AS quar,
 YEAR(r.callDate) AS ryear,
 ???????? AS scoreDateStart,
 (SELECT DATE (DATE_SUB( DATE_ADD( CONCAT( YEAR( r.callDate ), '-01-01'), INTERVAL QUARTER(r.callDate) QUARTER ), INTERVAL 1 DAY))) AS scoreDateEnd,
 group_concat(DISTINCT(r.resultId) separator ', ') AS resultIds
FROM results AS r
GROUP BY  quar, ryear
ORDER BY quar;

我尝试了谷歌搜索,但无济于事.

I have tried Googling but to no avail.

输出示例为:

'1', '2012', '2012-01-01', '2012-03-31', '57, 58, 59'
'2', '2012', '2012-04-01', '2012-06-30', '10549, 10551, 12598'

推荐答案

尝试一下:

要获取当前季度的开始日期,请使用此:

To get the start date of the current quarter use this:

  SELECT  MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE()) QUARTER 
                                       - INTERVAL    1 QUARTER 

因此您的查询将是:

SELECT
 QUARTER(r.callDate) AS quar,
 YEAR(r.callDate) AS ryear,
 MAKEDATE(YEAR(r.callDate), 1) + INTERVAL QUARTER(r.callDate) QUARTER -
  INTERVAL 1 QUARTER  AS scoreDateStart,
 (SELECT DATE (DATE_SUB( DATE_ADD( CONCAT( YEAR( r.callDate ), '-01-01'), 
 INTERVAL QUARTER(r.callDate) QUARTER ), INTERVAL 1 DAY))) AS scoreDateEnd,
 group_concat(DISTINCT(r.resultId) separator ', ') AS resultIds
FROM results AS r
GROUP BY  quar, ryear
ORDER BY quar;

这篇关于我如何在MySQL中获得四分之一的首次约会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆