Sql 在游戏节目数据库场景中,如何在单个查询中获取每个季度的平均总插曲分数?
请原谅戈尔这个头衔。我很难找到一个好的方式来表达我的问题,这是这个问题特有的 桌子 季节Sql 在游戏节目数据库场景中,如何在单个查询中获取每个季度的平均总插曲分数?,sql,postgresql,Sql,Postgresql,请原谅戈尔这个头衔。我很难找到一个好的方式来表达我的问题,这是这个问题特有的 桌子 季节 id name ---- ------ 1 Season 1 2 Season 2 3 Season 3 插曲 id season_id number title ---- ----------- -------- ------------------------------
id name
---- ------
1 Season 1
2 Season 2
3 Season 3
插曲
id season_id number title
---- ----------- -------- ---------------------------------------
1 1 1 Pilot
2 1 2 1x02 - We Got Picked Up
3 1 3 1x03 - This is the Third Episode
4 2 1 2x01 - We didn't get cancelled.
5 2 2 2x02 - We're running out of ideas!
6 3 1 3x01 - We're still here.
7 3 2 3x02 - Okay, this game show is dying.
8 3 3 3x03 - Untitled
id episode_id score contestant_id (table not given)
---- ------------ ------- ---------------------------------
1 1 35 1
2 1 -12 2
3 1 8 3
4 1 5 4
5 2 13 1
6 2 -2 5
7 2 3 3
8 2 -14 6
9 3 -14.5 1
10 3 -3 2
11 3 1.5 7
12 3 9.5 5
13 4 22.8 1
14 4 -3 8
15 5 2 1
16 5 13.5 9
17 5 7 3
18 6 13 1
19 6 -84 10
20 6 12 11
21 7 3 1
22 7 10 2
23 8 29 1
24 8 1 5
得分
id season_id number title
---- ----------- -------- ---------------------------------------
1 1 1 Pilot
2 1 2 1x02 - We Got Picked Up
3 1 3 1x03 - This is the Third Episode
4 2 1 2x01 - We didn't get cancelled.
5 2 2 2x02 - We're running out of ideas!
6 3 1 3x01 - We're still here.
7 3 2 3x02 - Okay, this game show is dying.
8 3 3 3x03 - Untitled
id episode_id score contestant_id (table not given)
---- ------------ ------- ---------------------------------
1 1 35 1
2 1 -12 2
3 1 8 3
4 1 5 4
5 2 13 1
6 2 -2 5
7 2 3 3
8 2 -14 6
9 3 -14.5 1
10 3 -3 2
11 3 1.5 7
12 3 9.5 5
13 4 22.8 1
14 4 -3 8
15 5 2 1
16 5 13.5 9
17 5 7 3
18 6 13 1
19 6 -84 10
20 6 12 11
21 7 3 1
22 7 10 2
23 8 29 1
24 8 1 5
正如你所看到的,你每一季有多集,每集有多个分数(每个参赛者一个分数)。参赛者可以在以后的剧集中再次出现(不相关),分数是浮点值,每集可以有任意数量的分数
那我在找什么呢?
我想得到每个季度的平均总插曲分数,其中总插曲分数是一个插曲中所有分数的总和。从数学上讲,这是一季所有得分的总和除以剧集数。很容易理解,但是我在一个查询中很难做到这一点并得到正确的结果。我希望输出如下所示:
name average_total_episode_score
---------- -----------------------------
Season 1 9.83
Season 2 21.15
Season 3 -5.33
顶级查询需要在季节
表上,因为它将与同一表上的其他类似查询组合在一起。在子查询中使用聚合很容易做到这一点,但是聚合执行子查询,无法满足我的单个查询要求。这可以在一个查询中完成吗?希望这能起作用
Select s.id, avg(score)
FROM Season S,
Episode e,
Score sc
WHERE s.id = e.season_id
AND e.id = sc.episode_id
Group by s.id
好吧,我想起来了。像往常一样,在简单的解决办法降临到我身上之前,我不得不写一整本书,然后把它贴出来 我的查询中的问题(我在问题中没有给出)是缺少
DISTINCT
计数。以下是一个工作查询:
SELECT
"season"."id",
"season"."name",
(SUM("score"."score") / COUNT(DISTINCT "episode"."id")) AS "average_total_episode_score"
FROM "season"
LEFT OUTER JOIN "episode"
ON ("season"."id" = "episode"."season_id")
LEFT OUTER JOIN "score"
ON ("episode"."id" = "score"."episode_id")
GROUP BY "season"."id"
选择Se.id作为季节id,总和(分数)作为季节分数,平均分数(分数)从分数S中加入S上的E集。E集id=E.id
加入se.id=e.Sessional\u id group by se.id在分数表中,您有插曲id。您如何知道哪个插曲将对应于特定的插曲?我在分数表中找不到任何季节id
score
s被键入一个插曲
,该插曲被键入一个季节
。我的问题是,在分数的插曲id列中有许多1。你怎么知道这是哪一季的第一集?得分。插曲id
指的是唯一的插曲.id
,而不是唯一的插曲.number
。我知道,被这么多数字弄糊涂了。这是一个实际数据库的简单表示,所以我只想包含足够的问题,而不是整个数据库。real插曲
表还包括列,如airdate
、date\u recorded
等。