1

我有 3 个模型(表):

Presentation hasMany PresentationView hasMany SlideView

领域:

介绍:id, title

演示视图:id, presentation_id

幻灯片视图:id, presentation_view_id, duration

我需要一个查询来获取每个演示文稿的统计信息。统计数据为:

  1. 每个 PresentationView 的数量
  2. 属于 Presentation 的幻灯片视图中所有 SlideView.duration 的总持续时间(通过 PresentationView)

所以基本上它看起来像双重 JOIN 和双重 GROUP 但连接对我不起作用 - 我尝试了 LEFT/INNER/RIGHT 双连接的每种组合,但我无法使其工作。我所拥有的最好的结果是 Presentation 已经对 PresentationView 进行了分组,但持续时间仅来自 SlideViews 的 SUMed,该 SlideViews 仅属于一个 PresentationViews 而不是全部用于 Presentation...

如果可能,我想避免嵌套 SELECT。只需加入/组

4

2 回答 2

1

首先是一个简单的 JOIN 和 COUNT:

SELECT p.id, COUNT(*)
FROM Presentation p
JOIN PresentationView v ON p.id = v.presentation_id
GROUP BY p.id

第二个必须使用 SUM(和 JOIN):

SELECT p.id, SUM(s.duration)
FROM Presentation p
JOIN PresentationView v ON p.id = v.presentation_id
JOIN SlideView s ON v.id = s.presentation_view_id
GROUP BY p.id

如果您想在一个查询中同时使用两者:

SELECT p.id, SUM(s.duration), COUNT(DISTINCT v.id)
FROM Presentation p
JOIN PresentationView v ON p.id = v.presentation_id
JOIN SlideView s ON v.id = s.presentation_view_id
GROUP BY p.id

原因DISTINCT

表:

Presentation:    PresentationView:          SlideView:
p.id | title     v.id | presentation_id     s.id | presentation_view_id | duration
1    | abc       1    | 1                   1    | 1                    | 100
2    | xyz       2    | 1                   2    | 1                    | 150
                 3    | 1                   3    | 2                    | 200
                 4    | 1                   4    | 2                    | 250
                 5    | 1                   5    | 3                    | 300
                 6    | 2                   6    | 3                    | 400
                 7    | 2                   7    | 4                    | 500
                                            8    | 5                    | 600
                                            9    | 6                    | 100
                                            10   | 6                    | 200
                                            11   | 7                    | 350

组之前的示例结果集:

p.id | v.id | s.id | s.duration
-------------------------------
1    | 1    | 1    | 100
1    | 1    | 2    | 150
1    | 2    | 3    | 200
1    | 2    | 4    | 250
1    | 3    | 5    | 300
1    | 3    | 6    | 400
1    | 4    | 7    | 500
1    | 5    | 8    | 600
2    | 6    | 9    | 100
2    | 6    | 10   | 200
2    | 7    | 11   | 350

在没有不同的组之后:

p.id | SUM | COUNT
------------------
1    | 8   | 2500
2    | 3   | 650

具有鲜明的:

p.id | SUM | COUNT
------------------
1    | 5   | 2500
2    | 2   | 650
于 2013-03-03T23:18:22.993 回答
0

与此同时,我找到了答案:

SELECT presentations.title, SUM(slide_views.duration), COUNT(DISTINCT presentation_views.id) 
FROM presentations 
LEFT JOIN presentation_views ON presentations.id = presentation_views.presentation_id 
LEFT JOIN slide_views ON presentation_views.id = slide_views.presentation_view_id 
GROUP BY presentations.id
于 2013-03-03T23:28:10.437 回答