0

I have query that counts the number of distinct values of a given field for a given week that where not recorder before. It counts the number of new values for the week.

SELECT COUNT(DISTINCT (field)) FROM table WHERE (creation BETWEEN CURDATE() - INTERVAL DAYOFWEEK(CURDATE()) + 5 DAY AND CURDATE() - INTERVAL DAYOFWEEK(CURDATE()) - 2 DAY) AND field NOT IN (SELECT DISTINCT (field) FROM table WHERE creation < CURDATE() - INTERVAL DAYOFWEEK(CURDATE()) + 5 DAY); 

I need to extend the query to be able to do the same computation for all the weeks in the past two months.

How can I do it?

1 Answer 1

1

Well, if I understand this correctly, a field occurs in some week for the first time. You want to count per week how many such first occurrences exist. So get the first date per field first. Then count per week.

select yearweek(first_occurence), count(*) from ( select field, min(creation) as first_occurence from table group by field ) fields group by yearweek(first_occurence) order by yearweek(first_occurence); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.