简体   繁体   中英

CTE Error - Error Code: 0, SQL State: 42P01

I'm attempting to create a CTE that displays the total amount of interviews that each churned customer has completed over a specified period of time.

Here is what I've come up w/ thus far:

     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

Currently, I'm receiving the following error message:

[Error Code: 0, SQL State: 42P01] ERROR: missing FROM-clause entry for table "completed_phone_interviews".

Each temporary result set successfully returns data so it appears that I'm not joining something correctly within the select statement that follows the CTE.

Any help would be greatly appreciated.

Your JOIN clauses in the main query are off. You try to join table organizations three times using the same alias against relations that are not yet defined in the main query:

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 

GROUP BY o.id, o.name, o.created_at;

Instead, you probably want:

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;

Otherwise, in the first and third completed_... CTEs you duplicate the org_id column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM