skip to Main Content

I am trying to write a query that counts the number of records that were created every 7 days,
Using this query i get the number of records created per day,

SELECT 'Closed' AS `Status`, COUNT(*) AS `Count`, `raisedon` AS `Date` FROM table WHERE raisedon >= '2019-01-01' and raisedon < '2019-03-29' AND status = 'Open' AND type = 'A' AND location = 'B' AND locationid = 'C' GROUP BY raisedon

This returns

Closed 1 2019-01-01
Closed 1 2019-01-14
Closed 2 2019-01-16
Closed 1 2019-01-24
Closed 1 2019-01-25
Closed 1 2019-01-30
Closed 1 2019-02-01
Closed 1 2019-02-03
Closed 1 2019-02-28
Closed 1 2019-03-07
Closed 1 2019-03-08

I would like the results to be like

Closed 1 2019-01-01
Closed 1 2019-01-08
Closed 2 2019-01-15
Closed 2 2019-01-22
Closed 3 2019-01-29
Closed 0 2019-02-05
Closed 0 2019-02-12
Closed 0 2019-02-19
Closed 1 2019-02-26
Closed 2 2019-03-05

Is this possible with just a query or will i have to use javascript aswell,
I am using phpmyadmin with mysql

Thanks for any advice

2

Answers


  1. One method uses datediff():

    SELECT 'Closed' AS `Status`,
           COUNT(*) AS `Count`,
           MIN(raisedon) AS `Date`
    FROM table
    WHERE raisedon >= '2019-01-01' AND
          raisedon < '2019-03-29' AND
          status = 'Open' AND
          type = 'A' AND
          location = 'B' AND
          locationid = 'C'
    GROUP BY FLOOR(DATEDIFF('2019-01-01', raisedon) / 7);
    
    Login or Signup to reply.
  2. If you are not too picky about your edge conditions for where a given week starts, you might just be able to aggregate using YEARWEEK:

    SELECT
        'Closed' AS Status,
        COUNT(*) AS cnt,
        YEARWEEK(raisedon) AS week
    FROM yourTable
    WHERE
        raisedon >= '2019-01-01' AND raisedon < '2019-03-29' AND
        status = 'Open' AND
        type = 'A' AND
        location = 'B' AND
        locationid = 'C'
    GROUP BY
        YEARWEEK(raisedon);
    

    This answer assumes that your data would have at least one data point present for each week. If there could be large gaps, then one solution would be to join with a calendar table. Here is an example of how to do this:

    SELECT
        'Closed' AS Status,
        COUNT(t2.raisedon) AS cnt,
        YEARWEEK(t1.raisedon) AS week
    FROM
    (
        SELECT '2019-01-01' AS raisedon UNION ALL
        SELECT '2019-01-02' UNION ALL
        SELECT '2019-01-03' UNION ALL
        ...
        SELECT '2019-01-31' UNION ALL
        SELECT '2019-02-01' UNION ALL
        ...
        SELECT '2019-03-31'
    ) t1
    LEFT JOIN yourTable t2
        ON t1.raisedon = t2.raisedon
    WHERE
        t1.raisedon >= '2019-01-01' AND t1.raisedon < '2019-03-29' AND
        t2.status = 'Open' AND
        t2.type = 'A' AND
        t2.location = 'B' AND
        t2.locationid = 'C'
    GROUP BY
        YEARWEEK(t1.raisedon);
    

    See here for several ways to generate a calendar table like the one used above in the second query.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search