获取多对多关系的聚合视图

时间:2014-09-01 06:32:43

标签: sql sqlite postgresql

我有一个包含3列与其他表的多对多关系的表。

示例:

主要

| id | brief_id | location_id | company_id | author_id |
|----|----------|-------------|------------|-----------|
| 1  | 22       | 4323        | 9876       | 9762      |
| 2  | 33       | 2675        | 6543       | 1643      |
| 3  | 44       | 2345        | 3445       |           |

中间表:

Location_int
| id | brief_id | location_id |
|----|----------|-------------|
| 1  | 22       | 5413        |
| 2  | 22       | 9833        |
| 3  | 22       | 2364        |

Company_int
| id | brief_id | company_id |
|----|----------|------------|
| 1  | 22       | 6231       |
| 2  | 22       | 6134       |
| 3  | 22       | 2364       |

Author_int
| id | brief_id | author_id |
|----|----------|-----------|
| 1  | 22       | 1345      |
| 2  | 22       | 9213      |
| 3  | 22       | 7255      |

加入表格

Location
| id   | location |
|------|----------|
| 5413 | Japan    |
| 9833 | India    |
| 2364 | Africa   |

Company
| id   | Company   |
|------|-----------|
| 5413 | Google    |
| 9833 | Microsoft |
| 2364 | Paypal    |

Author
| id   | Author |
|------|--------|
| 5413 | Tina   |
| 9833 | Saleen |
| 2364 | Sonny  |

我想要的是加入所有这些表并获得:

| brief_id | location             | company                   | Author             |
|----------|----------------------|---------------------------|--------------------|
| 22       | Japan, India, Africa | Google, Microsoft, Africa | Tina, Saleen, Sony |

地点,公司和作者 - 有时全部 - 都可以为null,在这种情况下我想看到空值。

我尝试过这样的左连接:

SELECT
  aur.brief_id,
  string_agg(l.name, ';'),
  string_agg(c.name, ';'),
  string_agg(a.name, ';')

FROM analytics_url_redirect aur
  LEFT JOIN location_int li
    ON bbl.brief_id = aur.brief_id
  LEFT JOIN location l
    ON l.id = li.location_id

  LEFT JOIN company_int ci
    ON bba.brief_id = aur.brief_id
  LEFT JOIN company c
    ON c.id = ci.agency_id

  LEFT JOIN author_int ai
    ON bba2.brief_id = aur.brief_id
  LEFT JOIN author a
    ON a.id = ai.author_id

GROUP BY aur.brief_id;

但是这给了我多次重复的地点,公司和作者。 类似的东西:

| brief_id | location                                                         | company                                              | Author             |
|----------|------------------------------------------------------------------|------------------------------------------------------|--------------------|
| 22       | Japan, India, Africa, Japan, India, Africa, Japan, India, Africa | Google, Microsoft, Africa, Google, Microsoft, Africa | Tina, Saleen, Sony |

实现这一目标的更好的查询是什么? (最好在PostgreSQL中) 同样指向可以很好地优化的查询。 仅供参考,所有列都已编入索引。

1 个答案:

答案 0 :(得分:0)

locationcompany等表格没有连接条件或它们之间的WHERE过滤器,因此加入它们会导致所有可能的行组合。

您必须在不同的子查询中计算每个摘要的其他对象:

SELECT brief_id,
       (SELECT group_concat(location)
        FROM Location_int AS li
        JOIN Location AS l ON li.location_id = l.id
        WHERE li.brief_id = aur.brief_id
       ) AS locations,
       (SELECT group_concat(Company)
        FROM Company_int AS ci
        JOIN Company AS c ON ci.company_id = c.id
        WHERE ci.brief_id = aur.brief_id
       ) AS companies,
       (SELECT group_concat(Author)
        FROM Author_int AS ai
        JOIN Author AS a ON ai.author_id = a.id
        WHERE ai.brief_id = aur.brief_id
       ) AS authors
FROM analytics_url_redirect AS aur

(对于PostgreSQL,将group_concat替换为string_agg。)