Say we have a table of Products
with the columns id, title, status, expires_at, created_at
.
I can get the values for single week by specifying all the parameters:
SELECT
count(id)
FROM
products
WHERE
status = 'APPROVED' AND
expires_at >= '2018-09-10 09:00:00' AND
created_at <= '2018-09-10 09:00:00' AND
deleted_at IS NULL;
How could we run a query that groups the products by weeks of the year 1-52 -- WHERE each week shows Products that were not expired: created_at <= week-end AND expires_at <= week-end
or active:
Active_Products | Week_Of_Year
----------------|---------------
274 | 2018-01
----------------|---------------
1011 | 2018-02
----------------|---------------
180 | 2018-03
----------------|---------------
990 | 2018-04
----------------|---------------
765 | 2018-05
I have updated this question with an SQL fiddle: http://sqlfiddle.com/#!9/e4830b/4