Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Wie bekomme ich das erste Datum eines Quartals in MySQL?

versuchen Sie Folgendes:

Verwenden Sie Folgendes, um das Startdatum des aktuellen Quartals zu erhalten:

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

Ihre Abfrage wäre also:

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;