简体   繁体   English

使用sum()的SQL查询

[英]SQL query using sum()

Hello I'm currently working on SQL problem that I can't quite figure out. 您好我正在研究SQL问题,我无法弄明白。 Here is the Schema I'm working with: 这是我正在使用的Schema:

在此输入图像描述

Here is the question I am stuck on: 这是我坚持的问题:

-- 3 Find the first name, last name and total combined film length of Sci-Fi films for every actor. - 3为每位演员查找科幻电影的名字,姓氏和总电影长度。 That is the result should list the names of all of the actors (even if an actor has not been in any Sci-Fi films) and the total length of Sci-Fi films they have been in. 这就是结果应该列出所有演员的名字(即使一个演员没有参加任何科幻电影)以及他们所在的科幻电影的总长度。

So far I have 到目前为止我有

SELECT actor.first_name, actor.last_name, (SELECT SUM(film.length)
from film 
INNER JOIN film_category
ON film.film_id = film_category.film_id
INNER JOIN category
ON film_category.category_id = category.category_id
INNER JOIN film_actor
ON film_actor.film_id = film.film_id
INNER JOIN actor
ON film_actor.actor_id = actor.actor_id
WHERE category.name = 'Sci-fi'
)
from actor

I know I need to group it by actor_id but i'm unable to do this in a select subquery. 我知道我需要通过actor_id对其进行分组,但我无法在select子查询中执行此操作。 Anyone have some tips? 有人有提示吗?

There is no need to use a subquery. 无需使用子查询。 Aggregate functions work on the entire data set. 聚合函数适用于整个数据集。 The 'group by' specifies how to group the data you're aggregating. 'group by'指定如何对您正在聚合的数据进行分组。

select a.actor_id, a.first_name, a.last_name, sum(f.length)
  from actor a
  left outer join film_actor fa on fa.actor_id   = a.actor_id
  left outer join film       f  on f.film_id     = fa.film_id
  left outer join film_categories       fc on fc.film_id    = f.film_id
  left outer join categories            c  on c.category_id = fc.category_id
 where c.name = 'sci-fi'
  group by a.actor_id
;

The outer joins ensure actors with no sci-fi film experience are included in the results by 外连接确保没有科幻电影经验的演员被包含在结果中

I don't understand what are you need subquery, try this: 我不明白你需要什么子查询,试试这个:

SELECT actor.first_name, actor.last_name,SUM(film.length)
from film 
INNER JOIN film_category
ON film.film_id = film_category.film_id
INNER JOIN category
ON film_category.category_id = category.category_id
INNER JOIN film_actor
ON film_actor.film_id = film.film_id
INNER JOIN actor
ON film_actor.actor_id = actor.actor_id
WHERE category.name = 'Sci-fi'
GROUP BY actor.actor_id;

This should get you exactly what you want, including the part about having actors that aren't in Sci-Fi movies. 这应该可以让你得到你想要的,包括有关于没有科幻电影的演员的部分。 You can LEFT JOIN on film to include all films the film_actor is in. The additional AND statement works with the LEFT JOIN to include actors not in Sci-Fi movies for your aggregate sum function. 您可以在电影中左键加入以包含film_actor所在的所有电影。附加的AND语句与LEFT JOIN一起使用,以包括不在Sci-Fi电影中的演员为您的总和功能。

SELECT a.actor_id, a.first_name, a.last_name, sum(f.length) AS length
FROM actor a
INNER JOIN film_actor fa ON fa.actor_id = a.actor_id
INNER JOIN film_category fc ON fc.film_id = fa.film_id
INNER JOIN category c ON c.category_id = fc.category_id
LEFT JOIN film f ON f.film_id = fa.film_id 
AND c.name = 'Sci-Fi'
GROUP BY a.actor_id; 

The top answer here actually is not correct. 这里的最佳答案实际上是不正确的。 This will work: 这将有效:

   SELECT T1.first_name, T1.last_name, T2.total
    FROM 
        (SELECT a.first_name, a.last_name, a.actor_id
         FROM actor a
         ) 
        AS T1
    LEFT JOIN 
        (SELECT a.first_name, a.last_name, a.actor_id, SUM( f.length ) AS total, c.name
        FROM actor a, film_actor fa, film f, film_category fc, category c
        WHERE c.category_id = fc.category_id
        AND fc.film_id = f.film_id
        AND a.actor_id = fa.actor_id
        AND fa.film_id = f.film_id
        AND c.name =  'sci-fi'
        GROUP BY a.actor_id) 
        AS T2 
    ON T1.actor_id = T2.actor_id;

It takes all actors, and combines them with the sci-fi actors and gives the combined screen time for sci-fi movies for all. 它需要所有演员,并将他们与科幻演员结合起来,为所有人提供科幻电影的合并屏幕时间。 Test it on your data set, worked for me. 在你的数据集上测试它,为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM