SQL Join Query帮助

时间:2011-08-10 18:11:09

标签: sql sql-server

我正在做一些SQL练习并遇到这个问题,下面的这个查询给了我一个“半正确”的结果,因为我只想显示标题最多的行,这个查询显示所有记录。有人可以帮忙吗?感谢。

问题:

这是'John Travolta'最繁忙的年份。显示他每年制作的电影数量。

表格

  • movie (id, title, yr, score, votes, director)
  • actor (id, name)
  • casting (movieid, actorid, ord)

查询:

select yr, max(title)
from 
(
select yr, count(title) title from movie
join casting
on (movie.id=casting.movieid)
join actor
on (casting.actorid=actor.id)
where actor.name="John Travolta"
group by yr Asc
) a

7 个答案:

答案 0 :(得分:3)

问题是

  

哪个是最繁忙的

...复数。那么,他的前五年是什么时候?

select top 5
 m.yr
,count(*)
from actor as a
    join casting as c
        join movie as m
        on m.movieid = c.movieid
    on c.actorid = a.actorid
where a.name = 'John Travolta'
group by
 m.yr
order by
 count(*) desc

然而,问题的第二部分指定您应该

  

显示他每年为制作的电影数量

到目前为止,我们的查询并没有考虑到John没有制作电影的年限......所以,这可能是你的半正确发挥作用的地方。也就是说,您可能想要创建一个表格变量,其中填充了从1954年到当前年份的年份值...并且离开了该表格。

declare @year table
(
    [yr] int
)
declare @currentYear int = datepart(year,getdate())
while @currentYear >= 1954 begin -- Travolta was born in 1954!

    insert @year values (@currentYear)
    set @currentYear -= 1

end

select
 y.yr
,count(m.movieid)
from @year y
    left join movies as m
        join casting as c
            join actor as a
            on a.actorid = c.actorid
            and a.name = 'John Travolta'
        on c.movieid = m.movieid
    on m.yr = y.yr
group by
 y.yr
order by
,count(m.movieid) desc

[编辑:根据评论] 以及最终查询,以返回其计数与任何一年中最高的年份匹配的所有年份。

;with TravoltaMovies as
(
    select
     m.yr
    ,count(*) as [Count]
    from actor as a
        join casting as c
            join movie as m
            on m.movieid = c.movieid
        on c.actorid = a.actorid
    where a.name = 'John Travolta'
    group by m.yr
)
select
*
from TravoltaMovies as tm
where tm.[Count] = (select max([Count]) from TravoltaMovies)

答案 1 :(得分:1)

答案是:

SELECT y.yr,MAX(y.count)
FROM(
SELECT movie.yr,COUNT(movie.yr) AS count
FROM (movie JOIN casting ON (movie.id=movieid)) JOIN actor ON (actor.id=actorid)
WHERE name='John Travolta'
GROUP BY yr
ORDER BY COUNT(movie.yr) DESC) y

答案 2 :(得分:1)

这是一个非常简单的解决方案。

select yr, count(yr)  
from movie 
join casting on movie.id = movieid
join actor on actorid = actor.id
where name = 'John Travolta' 
group by yr
having count(yr) > 2** 

乐意帮助

答案 3 :(得分:0)

select TOP 1 yr, title
from 
(
select yr, count(title) title from movie
join casting
on (movie.id=casting.movieid)
join actor
on (casting.actorid=actor.id)
where actor.name="John Travolta"
group by yr Asc
) a
ORDER BY title DESC

只需添加TOP选项和ORDER BY

汇总是不必要的。

答案 4 :(得分:0)

感谢所有这些是查询:

SELECT yr,COUNT(title) FROM
  movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
where name='John Travolta'
GROUP BY yr
HAVING COUNT(title)=(SELECT MAX(c) FROM
(SELECT yr,COUNT(title) AS c FROM
   movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
 where name='John Travolta'
 GROUP BY yr) AS t
)

答案 5 :(得分:0)

这是一个使用连接和子查询概念的简单答案

select yr,count(title) from movie inner join casting on movie.id=casting.movieid where actorid= (select id from actor where name ='John Travolta') group by yr having count(title)>2

答案 6 :(得分:0)

这是一个更简单的解决方案 -

  1. 首先,我们将所有表格连接起来
  2. 然后我们在名称上放置了一个类别过滤器,名称为''John Travolta'
  3. 现在我们在yr上放置一个group函数,这样我们就有了yr和相应的count(yr)
  4. 解决方案希望只有数年> 2的年份,因此我们使用'having'子句对分组数据应用该过滤器

    select yr, count(yr) from movie join casting on movie.id=movieid join actor on actor.id=actorid where name= 'John Travolta' group by yr having count(yr)>2

  5. 希望这会有所帮助,我认为没有必要为此编写程序。