How to get the data of each regular week?

Viewed 38

I would like to display data for each week of the current month. What I did was

# Week 1
with connection.cursor() as cursor:
    sql = 'SELECT datecreation, COUNT(panelname) ' \
          'FROM panneau ' \
          'WHERE month(datecreation) = month(now()) ' \
          'AND year(datecreation) = year(now()) ' \
          'AND date BETWEEN date_sub(now(), INTERVAL 1 WEEK) AND now()'
    cursor.execute(sql)
    numberpanels = cursor.fetchall()

# Week 2
with connection.cursor() as cursor:
    sql = 'SELECT datecreation, COUNT(panelname) ' \
          'FROM panneau ' \
          'WHERE month(datecreation) = month(now()) ' \
          'AND year(datecreation) = year(now()) ' \
          'AND date BETWEEN date_sub(now(), INTERVAL 2 WEEK) AND now()'
    cursor.execute(sql)
    numberpanels = cursor.fetchall()

# Week 3
with connection.cursor() as cursor:
    sql = 'SELECT datecreation, COUNT(panelname) ' \
          'FROM panneau ' \
          'WHERE month(datecreation) = month(now()) ' \
          'AND year(datecreation) = year(now()) ' \
          'AND date BETWEEN date_sub(now(), INTERVAL 3 WEEK) AND now()'
    cursor.execute(sql)
    numberpanels = cursor.fetchall()

# Week 4
with connection.cursor() as cursor:
    sql = 'SELECT datecreation, COUNT(panelname) ' \
          'FROM panneau ' \
          'WHERE month(datecreation) = month(now()) ' \
          'AND year(datecreation) = year(now()) ' \
          'AND date BETWEEN date_sub(now(), INTERVAL 4 WEEK) AND now()'
    cursor.execute(sql)
    numberpanels = cursor.fetchall()

# Week 5
with connection.cursor() as cursor:
    sql = 'SELECT datecreation, COUNT(panelname) ' \
          'FROM panneau ' \
          'WHERE month(datecreation) = month(now()) ' \
          'AND year(datecreation) = year(now()) ' \
          'AND date BETWEEN date_sub(now(), INTERVAL 5 WEEK) AND now()'
    cursor.execute(sql)
    numberpanels = cursor.fetchall()

The problem with this code is : it displays the past 7, 14, 21, 28, 31 days of data but not the data of a "regular" week (I don't know what to call it), which means starting the Monday and ending Sunday. Currently, If I am Wednesday, it will get the data down to the previous Wednesday (for the Week 1 query).

What I would want is to get is the data of each working week of the current month. If the first day of the month starts a Wednesday, I want the first week to get the data from Wednesday, Thursday, Friday, Saturday, Sunday. And so forth for the next weeks.

Any help would be welcome, thank you.

0 Answers
Related