select to_char("time"::DATE-(extract(dow from "time"::TIMESTAMP)-1||'day')::interval, 'YYYY-mm-dd') d,
count(1),count(distinct username) from t_audit where sysname in %s and "time" between %s and %s
GROUP BY d order by d;
这个就是按周聚合的代码,但是注意这个结果集的d是一周的第一天。