PostgreSQL:查询过去4周的总数,并将结果与​​前4周的时间进行比较

时间:2016-03-10 20:23:30

标签: sql postgresql

我有一个返回四周和八周总计/平均值的查询。虽然这很有用,但我更感兴趣的是评估过去四周的表现以及该数据与前四周(即前八周)的比较情况。任何和所有的帮助将非常感激。

SELECT COUNT (*) AS auto_applicants_4_weeks
   ,(SELECT ROUND(AVG (cjs.score):: numeric,1) 
     FROM candidate_jobs c
              LEFT JOIN organizations o ON o.id = c.organization_id
              LEFT JOIN candidate_job_surveys cjs ON cjs.candidate_job_id = c.id
      WHERE (cjs.date_completed>= date_trunc('week', CURRENT_TIMESTAMP - interval '4 weeks') 
               and cjs.date_completed < date_trunc('week',CURRENT_TIMESTAMP))
               and o.vertical = 'Auto'
               and cjs.is_completed = 'True'
                  GROUP BY o.vertical) as four_week_smartrank_auto_average
 ,(SELECT COUNT (*) 
     FROM candidate_jobs c
            LEFT JOIN organizations o ON o.id = c.organization_id
     WHERE (c.created_at>= date_trunc('week', CURRENT_TIMESTAMP - interval '8 weeks') 
            and c.created_at < date_trunc('week',CURRENT_TIMESTAMP))
            and o.vertical = 'Auto'
                GROUP BY o.vertical) as auto_applicants_eight_weeks
 ,(SELECT ROUND(AVG (cjs.score):: numeric,1) 
     FROM candidate_jobs c
             LEFT JOIN organizations o ON o.id = c.organization_id
             LEFT JOIN candidate_job_surveys cjs ON cjs.candidate_job_id = c.id
     WHERE (cjs.date_completed>= date_trunc('week', CURRENT_TIMESTAMP - interval '8 weeks') 
        and cjs.date_completed < date_trunc('week',CURRENT_TIMESTAMP))
        and o.vertical = 'Auto'
        and cjs.is_completed = 'True'
            GROUP BY o.vertical) as eight_week_smartrank_auto_average
 ,(SELECT COUNT (c.id) 
       FROM candidate_jobs c
               LEFT JOIN organizations o ON o.id = c.organization_id
       WHERE (c.created_at>= date_trunc('week', CURRENT_TIMESTAMP - interval '4 weeks') 
               and c.created_at < date_trunc('week',CURRENT_TIMESTAMP))
               and o.crm_id = '001U000001aEIuuIAG'
              GROUP BY o.crm_id )as dealership_auto_applicants_4_weeks
 ,(SELECT ROUND(AVG (cjs.score):: numeric,1) 
      FROM candidate_jobs c
              LEFT JOIN organizations o ON o.id = c.organization_id
              LEFT JOIN candidate_job_surveys cjs ON cjs.candidate_job_id = c.id
        WHERE (cjs.date_completed>= date_trunc('week', CURRENT_TIMESTAMP - interval '4 weeks') 
              and cjs.date_completed < date_trunc('week',CURRENT_TIMESTAMP))
              and o.crm_id = '001U000001aEIuuIAG'
              and cjs.is_completed = 'True'
              GROUP BY o.crm_id) as dealership_smartrank_avg_4_weeks
 ,(SELECT COUNT (c.id) 
      FROM candidate_jobs c
              LEFT JOIN organizations o ON o.id = c.organization_id
      WHERE (c.created_at>= date_trunc('week', CURRENT_TIMESTAMP - interval '8 weeks') 
              and c.created_at < date_trunc('week',CURRENT_TIMESTAMP))
              and o.crm_id = '001U000001aEIuuIAG'
             GROUP BY o.crm_id )as dealership_auto_applicants_8_weeks
 ,(SELECT ROUND(AVG (cjs.score):: numeric,1) 
 FROM candidate_jobs c
          LEFT JOIN organizations o ON o.id = c.organization_id
          LEFT JOIN candidate_job_surveys cjs ON cjs.candidate_job_id = c.id
 WHERE (cjs.date_completed>= date_trunc('week', CURRENT_TIMESTAMP - interval '8 weeks') 
         and cjs.date_completed < date_trunc('week',CURRENT_TIMESTAMP))
         and o.crm_id = '001U000001aEIuuIAG'
         and cjs.is_completed = 'True'
         GROUP BY o.crm_id) as dealership_smartrank_avg_8_weeks
FROM candidate_jobs c
       LEFT JOIN organizations o ON o.id = c.organization_id
 WHERE (c.created_at>= date_trunc('week', CURRENT_TIMESTAMP - interval '4 weeks') 
        and c.created_at < date_trunc('week',CURRENT_TIMESTAMP))
        and o.vertical = 'Auto'
        GROUP BY o.vertical

0 个答案:

没有答案