无法确定如何计算SQL中的某些值

时间:2013-10-08 07:07:46

标签: sql

我有三张桌子:

ActorInfo(actorID, firstname, lastname)
CastInfo(movieID, actorID)
GenreInfo(movieID, genre)

我正在尝试编写一个SQL查询,它将返回一个演员的详细信息,以及他们已经加入的“戏剧”电影的数量,而不会重复。

所以,理想情况下,我会有类似的东西,

1 George Clooney 10
2 Brad Pitt 8
3 Matt Damon 6

所以,我的代码如下:

SELECT actorinfo.actorID, actorinfo.firstname, actorinfo.lastname, COUNT(DISTINCT actorinfo.actorID) as total
FROM actorinfo
LEFT OUTER JOIN castinfo
ON actorinfo.actorID= castinfo.actorID
LEFT OUTER JOIN genreinfo
ON castinfo.mvID = genreinfo.mvID
WHERE genreinfo.genre = 'Drama'
GROUP BY actorinfo.actorID, actorinfo.firstname, actorinfo.lastname
ORDER BY total ASC;

然而,这只是为所有演员返回1的'total'结果。我真的在这里遇到了障碍,我不确定如何解决这个问题。

4 个答案:

答案 0 :(得分:1)

只需删除您正在计算的列上的不同。

SELECT actorinfo.actorID, 
actorinfo.firstname, 
actorinfo.lastname, 
COUNT(actorinfo.actorID) as total
FROM ActorInfo as actorinfo
LEFT OUTER JOIN CastInfo as castinfo
ON actorinfo.actorID= castinfo.actorID
LEFT OUTER JOIN GenreInfo as genreinfo
ON castinfo.mvID = genreinfo.mvID
WHERE genreinfo.genre = 'Drama'
GROUP BY actorinfo.actorID
ORDER BY total ASC;

http://sqlfiddle.com/#!2/29364/6

您不希望计算组中id值的不同出现次数,因为您分组的ID显然总是在组中具有相同的值。或者,您可以对genreinfo的id和actor的id进行分组,并计算组内的所有记录,更加自我解释。

SELECT actorinfo.actorID, 
actorinfo.firstname, 
actorinfo.lastname, 
COUNT(*) as total
FROM ActorInfo as actorinfo
LEFT OUTER JOIN CastInfo as castinfo
ON actorinfo.actorID= castinfo.actorID
LEFT OUTER JOIN GenreInfo as genreinfo
ON castinfo.mvID = genreinfo.mvID
WHERE genreinfo.genre = 'Drama'
GROUP BY genreinfo.mvId,actorinfo.actorID
ORDER BY total ASC;

如果您有与特定电影相关的信息,我想这将转到演员与流派之间的关系所持有的castinfo。在这种情况下,您应该执行以下操作以避免重复,

SELECT actorinfo.actorID, 
actorinfo.firstname, 
actorinfo.lastname, 
COUNT(DISTINCT castinfo.movie_name) as total
FROM ActorInfo as actorinfo
LEFT OUTER JOIN CastInfo as castinfo
ON actorinfo.actorID= castinfo.actorID
LEFT OUTER JOIN GenreInfo as genreinfo
ON castinfo.mvID = genreinfo.mvID
WHERE genreinfo.genre = 'Drama'
GROUP BY genreinfo.mvId,actorinfo.actorID
ORDER BY total ASC;

http://sqlfiddle.com/#!2/aa4363/1

答案 1 :(得分:0)

按以下方式尝试查询。希望这有效......

SELECT actorinfo.actorID, actorinfo.firstname, actorinfo.lastname, COUNT(actorinfo.actorID) as total FROM actorinfo 
LEFT OUTER JOIN castinfo
ON actorinfo.actorID= castinfo.actorID
LEFT OUTER JOIN genreinfo
ON castinfo.mvID = genreinfo.mvID
WHERE genreinfo.genre = 'Drama'
GROUP BY actorinfo.actorID, actorinfo.firstname, actorinfo.lastname
ORDER BY total ASC;

答案 2 :(得分:0)

如果没有详细的数据模型知识,就会有一些猜测。这应该提供可靠的电影数量“无需重复”。但是,如果演员可以针对单个电影多次列出(例如,如果在该电影中扮演多个角色),则可能是必要的。

SELECT
        actorinfo.actorID
      , actorinfo.firstname
      , actorinfo.lastname
      , COUNT(DISTINCT castinfo.mvID) AS total /* distinct of movie id's */
FROM ActorInfo AS actorinfo
LEFT OUTER JOIN CastInfo AS castinfo
        ON actorinfo.actorID = castinfo.actorID
LEFT OUTER JOIN GenreInfo AS genreinfo
        ON castinfo.mvID = genreinfo.mvID
WHERE genreinfo.genre = 'Drama'
GROUP BY 
        actorinfo.actorID
      , actorinfo.firstname
      , actorinfo.lastname
ORDER BY
        total ASC
      , actorinfo.firstname
      , actorinfo.lastname
;

答案 3 :(得分:0)

对于Sql-Server 2005及更高版本( Fiddle Demo ):

;With CTE as (
   Select c.actorID, count(distinct c.movieID) mCount
   From CastInfo c 
      join GenreInfo g on c.movieID = g.movieID
   Where g.genre = 'Drama'
   Group by c.actorID
)
Select a.actorID, a.firstName, a.lastName, coalesce(cte.mCount,0) total
from actorInfo a
      left join cte on a.actorID = cte.actorID
Order by total