我有一个查询,它计算给定周的给定字段的不同值的数量。它计算了本周新值的数量。
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);
我需要扩展查询,以便能够在过去两个月的所有周内执行相同的计算。
我该怎么做?
答案 0 :(得分:1)
好吧,如果我理解正确的话,第一次就会在某个星期内出现一个字段。您希望每周计算存在多少次此类事件。因此,首先获得每个字段的第一个日期。然后每周计算。
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);