sql—避免重复2个左联接表中的聚合列值

vdgimpew  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(385)

我有5个表,其中包含如下行:
研究报告表:

REPORT_ID   TOPIC
141         My Report Topic Title
142         Another Report Topic Title
143         Yet Another Report Topic Title
...

程序区域报告关系表:

REPORT_ID    PROGRAM_AREA_ID
141          6
141          11
141          12
...

程序区域表:

PROGRAM_AREA_ID    TITLE
6                  Program Area One
11                 Program Area Two
12                 Program Area Three
...

研究报告分类关系表:

REPORT_ID    CATEGORY_ID
141          9
141          10
141          18
141          23
...

研究报告分类表:

CATEGORY_ID    NAME
9              Category One
10             Category Two
18             Category Three
23             Category Four
...

此查询当前正在从下面返回结果:

SELECT rr.report_id, 
rr.topic, 
string_agg(pa.title, '|') as program_areas, 
string_agg(rrc.name, '|') as categories 
FROM RESEARCH_REPORTS rr 
LEFT JOIN PROGRAM_AREAS_REPORTS_REL parr ON rr.report_id = parr.report_id 
LEFT JOIN RESEARCH_REPORT_CATEGORY_REL rrcr ON rr.report_id = rrcr.report_id 
LEFT JOIN PROGRAM_AREAS pa ON parr.program_area_id = pa.program_area_id 
LEFT JOIN RESEARCH_REPORT_CATEGORIES rrc ON rrcr.category_id = rrc.category_id 
WHERE rr.report_id = 141
GROUP BY rr.report_id, rr.topic

查询结果:

|---------------------|-------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|      report_id      |                topic                |                      program_areas                                                                                                                                |                                                                                     categories                                                                                                                                    |
|---------------------|-------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|          141        |         My Report Topic Title       |   Program Area One|Program Area Two|Program Area Three|Program Area One|Program Area Two|Program Area Three|Program Area One|Program Area Two|Program Area Three  |    Category One|Category Two|Category Three|Category Four|Category One|Category Two|Category Three|Category Four|Category One|Category Two|Category Three|Category Four|Category One|Category Two|Category Three|Category Four    |
|---------------------|-------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

如果注意到,结果列中的聚合值 program_areas 以及 categories 都是重复的。我需要使用select查询避免这些重复,该查询的结果格式如下:
首选查询结果:

|---------------------|-------------------------------------|---------------------------------------------------------|--------------------------------------------------------------|
|      report_id      |                topic                |                      program_areas                      |                       categories                             |
|---------------------|-------------------------------------|---------------------------------------------------------|--------------------------------------------------------------|
|          141        |         My Report Topic Title       |   Program Area One|Program Area Two|Program Area Three  |    Category One|Category Two|Category Three|Category Four    |
|---------------------|-------------------------------------|---------------------------------------------------------|--------------------------------------------------------------|

如何在当前查询中完成此操作?

crcmnpdw

crcmnpdw1#

你可以试试这个

SELECT  rr.report_id
        , rr.topic
        , ( SELECT  string_agg(pa.title, '|')
            FROM    PROGRAM_AREAS pa
            JOIN    PROGRAM_AREAS_REPORTS_REL parr
            ON      parr.program_area_id = pa.program_area_id 
            WHERE   parr.report_id = rr.report_id
            GROUP BY parr.report_id ) as program_areas
        , ( SELECT  string_agg(rrc.name, '|')
            FROM    RESEARCH_REPORT_CATEGORIES rrc 
            JOIN    RESEARCH_REPORT_CATEGORY_REL rrcr
            ON      rrcr.category_id = rrc.category_id 
            WHERE   rrcr.report_id = rr.report_id
            GROUP BY rrcr.report_id ) as categories
FROM    RESEARCH_REPORTS rr 
WHERE   rr.report_id = 141

但我不知道如何设置由字符串\u agg连接的名称/标题的顺序。

z3yyvxxp

z3yyvxxp2#

尝试此查询

SELECT rr.report_id, rr.topic, 
STUFF((SELECT ' | '+ PROGRAM_AREAS.title FROM PROGRAM_AREAS FOR XML PATH('')),1,2,'') AS program_areas,
STUFF((SELECT ' | '+ RESEARCH_REPORT_CATEGORIES.NAME FROM RESEARCH_REPORT_CATEGORIES FOR XML PATH('')),1,2,'') AS categories
FROM RESEARCH_REPORTS rr 
LEFT JOIN PROGRAM_AREAS_REPORTS_REL parr ON rr.report_id = parr.REPORT_ID 
LEFT JOIN RESEARCH_REPORT_CATEGORY_REL rrcr ON rr.report_id = rrcr.report_id 
WHERE rr.report_id = 141
GROUP BY rr.report_id, rr.topic

相关问题