2

月ごとに 2 つの日付フィルターの間で日をグループ化しようとしています。mysqlでこれを行うことは可能ですか。

Example : StartDate : 2012-01-19  EndDate: 2012-03-24

クエリは、月ごとにグループ化された日を返す必要があります

Jan : 19
Feb  : 29
Mar :24
Apr : 0
May : 0 etc

これを行う方法はありますか?

ありがとう!

4

2 に答える 2

3

このクエリは、限られた日付範囲 (過去 1,00,000 日) で機能します。WHILEただし、ループを使用して別の関数またはプロシージャを作成することをお勧めします。

SELECT DATE_FORMAT(_date, '%M') AS month,
       COUNT(1) AS days
FROM (
        SELECT CURDATE() - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) + (10000 * e.a)) DAY AS _date
        FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS d
        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS e
     ) a
WHERE _date BETWEEN '2012-01-19' AND '2012-03-24'
GROUP BY MONTH(_date)
ORDER BY MONTH(_date);

コメントの質問に対する新しい回答: 質問がありました。このクエリを使用して別のテーブルに JOIN を実行するにはどうすればよいですか。基本的な考え方は、startDate と endDate を持つ Bookings テーブルがあり、これらを _date BETWEEN のパラメーターとして使用して、各月の日数を調べる必要があるということです。どうすればそれができるかについての提案はありますか? また、複数の行ではなく単一の行で結果を返すことも可能です:

ここでSQL FIDDLE DEMOを参照してください:

CREATE TABLE Bookings (
  start_date date DEFAULT NULL,
  end_date date DEFAULT NULL
);
INSERT INTO Bookings(start_date, end_date)VALUES('2012-01-19','2012-03-24'),('2012-01-05','2012-08-21');

SELECT b.*,
(
 SELECT CONCAT(DATE_FORMAT(b.start_date, '%M:'), (DATEDIFF(LAST_DAY(b.start_date), b.start_date) + 1), ',',
               GROUP_CONCAT(CONCAT(DATE_FORMAT(first_day, '%M:'),days)), ',',
               DATE_FORMAT(b.end_date, '%M:%d')
              ) AS total_days
FROM(
             SELECT DATE_FORMAT(_date, '%Y-%m-01') AS first_day,
                    COUNT(1) AS days
             FROM (
                    SELECT CURDATE() - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) + (10000 * e.a)) DAY AS _date
                    FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
                    CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
                    CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
                    CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS d
                    CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS e
                 ) a, (SELECT @cnt := 0) b
              GROUP BY YEAR(_date), MONTH(_date)
             ) a
             WHERE first_day BETWEEN DATE_ADD(LAST_DAY(b.start_date), INTERVAL 1 DAY) AND DATE_SUB(b.end_date, INTERVAL 1 MONTH)
        ) total_days
FROM Bookings b
于 2012-08-20T10:08:33.830 に答える