Postgres string_agg按从父级到子级分组到3级

时间:2015-09-09 11:39:33

标签: sql postgresql subquery views

我的数据库中有这些表。

    Table applications(
    application_id [PK], 
    resume_id [FK from resume table], 
    user_id [FK from user table]
    )

    Table resume ( 
    resume_id [PK],
    resume_name 
    );

    Table education (
    education_id [PK]
    resume_id [FK from resume]
    education_title,
    education_institute
    );

    Table experience (
    experience_id [Pk],
    resume_id [FK]
    experience_company,
    experience_designation
    );

application_id  | resume_id | education__titles | experience_designation
------------------------------------------------------------------------
    1           |  35       | BS, MSCS, PHD     | Developer, Team Lead
    1           |  44       | BA, Masters in Eng| Teacher, Instructior
    1           |  57       | FA, Matric, BA Art| Instructor, Developer

实际上,我希望获得所有简历,并将教育和经验字段作为一行与一个应用程序ID相对应。

到目前为止,我已经写了这个查询。

SELECT r.resume_id as resume_id, r.resume_name as resume_name, 
string_agg(distinct(edu.title), ', ') as edu_degree_titles, 
string_agg(distinct(edu.institute), ', ') as education_institute, 
string_agg(distinct(exp.designation), ', ') as experience_designation,
string_agg(distinct(exp.company_name), ', ') as experience_company_name
FROM jobpost_jobapplication job_app, user_resume_resume r, user_resume_education edu, user_resume_experience exp
WHERE job_app.app_id = 15011
AND job_app.resume+id = r.resume_id
AND r.resume_id = edu.resume_id
AND r.resume_id = exp.resume_id

但只要我把下面的应用程序表显示在内,就说我必须从简历表中对resume_id进行分组。

SELECT app.app_id  as application_id 
FROM application app
LEFT JOIN 
(
SELECT r.resume_id as resume_id, r.resume_name as resume_name, 
string_agg(distinct(edu.title), ', ') as education__titles, 
string_agg(distinct(edu.institute), ', ') as education_institute, 
string_agg(distinct(exp.designation), ', ') as experience_designation,
string_agg(distinct(exp.company_name), ', ') as experience_company_name
FROM user_resume_resume r, user_resume_education edu, user_resume_experience exp
WHERE r.resume_id = edu.resume_id
AND r.resume_id = exp.resume_id
GROUP BY r.resume_id
) P
ON P.resume_id = app.resume_id
WHERE app.app_id = 18163;

它什么都不返回......

0 个答案:

没有答案