CTE错误 - 错误代码:0,SQL状态:42P01

时间:2016-01-27 15:18:41

标签: sql postgresql common-table-expression

我正在尝试创建一个CTE,显示每个流失客户在指定时间段内完成的访谈总数。

到目前为止,这就是我的目标:

     WITH chg_acct(acct_id, org_id, name, old_status, new_status, seq_desc, seq_asc, crm_id, cr, vertical) as (
        SELECT a.id, o.id, o.name, s.old_value, s.new_value,
        rank() over (partition by a.id order by s.created_at desc),
        rank() over (partition by a.id order by s.created_at),
        crm_id, s.created_at, o.vertical
        FROM accounts a
        JOIN
        organizations o on o.id = a.organization_id
        JOIN
        slowly_changing_dimensions s
        ON s.resource_id = a.id AND s.resource_table_name = 'accounts' AND s.resource_attribute = 'status'
        WHERE s.created_at::date BETWEEN '2015-01-01' AND '2016-01-25'
        AND o.name NOT LIKE 'ZZ%' and lower(o.name) NOT LIKE '%lino%'),

        canceled_orgs (org_id) as (
        SELECT a.org_id
        FROM
        chg_acct a
        inner join chg_acct b ON a.acct_id = b.acct_id
        WHERE a.seq_desc = 1 AND b.seq_asc = 1
        AND a.old_status <> b.new_status AND b.new_status = 'CANCELED'),

        completed_elements_interviews (org_id,total_completed_elements_interviews)  as (
        SELECT 
        cj.organization_id org_id,
        COUNT (score) total_completed_elements_interviews,
        cj.organization_id org_id
          FROM interview_documents i
          JOIN candidate_jobs cj on i.candidate_job_id = cj.id
        WHERE score IS NOT NULL
        AND interview_type_id = 4
        GROUP BY org_id),

        completed_achievement_interviews (org_id,total_completed_achievements_interviews) as (
        SELECT 
        cj.organization_id org_id,
        COUNT (a.is_completed) total_completed_achievements_interviews 
          FROM achievement_screens a
          JOIN candidate_jobs cj on a.candidate_job_id = cj.id
        WHERE a.is_completed = 'true'
        GROUP BY org_id),

        completed_phone_interviews (org_id,total_completed_phone_interviews) as (
        SELECT 
        cj.organization_id org_id,
        COUNT (score) total_completed_phone_interviews,
        cj.organization_id org_id
          FROM interview_documents i
          JOIN candidate_jobs cj on i.candidate_job_id = cj.id
        WHERE score IS NOT NULL
        AND interview_type_id = 1
        GROUP BY org_id)

      SELECT o.name, o.created_at, o.id,total_completed_phone_interviews,total_completed_achievements_interviews,      
total_completed_element_interviews
FROM canceled_orgs a 
LEFT JOIN organizations o ON completed_phone_interviews.org_id = o.id
LEFT JOIN organizations o ON completed_achievements_interviews.org_id = o.id
LEFT JOIN organizations o ON completed_elements_interviews.org_id = o.id
group by o.id, o.name, o.created_at

目前,我收到以下错误消息:

[错误代码:0,SQL状态:42P01]错误:缺少表“completed_phone_interviews”的FROM子句条目。

每个临时结果集都成功返回数据,因此看起来我没有在CTE后面的select语句中正确连接。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

主查询中的JOIN子句已关闭。您尝试使用相同的别名连接表organizations三次,而不是在主查询中尚未定义的关系:

WITH (lots_of_CTEs)
SELECT o.name, o.created_at, o.id, total_completed_phone_interviews,
       total_completed_achievements_interviews, total_completed_element_interviews
FROM canceled_orgs a 
LEFT JOIN organizations o ON completed_phone_interviews.org_id = o.id
LEFT JOIN organizations o ON completed_achievements_interviews.org_id = o.id
LEFT JOIN organizations o ON completed_elements_interviews.org_id = o.id
GROUP BY o.id, o.name, o.created_at;

相反,你可能想要:

WITH (lots_of_CTEs)
SELECT o.name, o.created_at, o.id, p.total_completed_phone_interviews,
       a.total_completed_achievements_interviews, e.total_completed_element_interviews
FROM canceled_orgs c
LEFT JOIN organizations o ON o.id = c.org_id
LEFT JOIN completed_phone_interviews p ON p.org_id = o.id
LEFT JOIN completed_achievements_interviews a ON a.org_id = o.id
LEFT JOIN completed_elements_interviews e ON e.org_id = o.id
GROUP BY o.id, o.name, o.created_at;

否则,在第一个和第三个completed_... CTE中,您复制了org_id列。