如何过滤掉PostgreSQL中的周内数据?

时间:2017-08-14 16:39:22

标签: sql postgresql date filter

我有一个查询,我正在利用仪表板可视化。查询的唯一问题是它包含当前周的数据 - 这是有问题的,因为周未完全出炉。理想情况下,我想添加一个过滤器,该过滤器在过去52周内提取数据,但不包括日期大于最近报告周(周日 - 周六)的数据。 这是我现在拥有的:

SELECT (DATE_TRUNC('week',cj.created_at:: timestamptz) + '5 days':: 
         interval)::date
       ,CASE WHEN o.vertical IS NULL OR o.vertical NOT IN 
        ('Auto','Franchise') THEN 'SMB'
       ELSE o.vertical END as vertical
       ,COUNT(DISTINCT cj.native_candidate_job_id) as applicant_traffic
FROM dim_candidate_jobs cj
JOIN dim_organizations o ON cj.native_organization_id = 
     o.native_organization_id
WHERE o.demo = false
    AND NOT(o.name ~~* any (array['%test%','%api%']))
    AND cj.created_at:: date > current_date - interval '52 weeks'
    AND cj.created_at:: date < (DATE_TRUNC('week',current_date:: 
         timestamptz) + '1 day':: interval)::date
    AND o.current = 'Y'
    AND cj.current = 'Y'
GROUP BY 1,2;;

1 个答案:

答案 0 :(得分:0)

and cj.created_at >= date_trunc('week', current_date) - interval '52 weeks'
and cj.created_at < date_trunc('week', current_date)