简体   繁体   English

SQL Server:多个事务

[英]SQL Server : multiple transactions

select 
    STUDIO.NAME, MOVIE.TITLE, MOVIE.YEAR
from 
    STUDIO 
join 
    MOVIE on STUDIO.NAME = MOVIE.STUDIONAME
where 
    MOVIE.YEAR >= ALL (select MOVIE.YEAR from MOVIE)

I have this code which give me as a result the year of the last film, it's title and the name of the studio, which make the movie. 我有这段代码,可以给我最后一部电影的年份,标题和制作电影的工作室名称。

How can I rewrite this code, so I can get the last produced movie by each studio, not only by one? 如何重写此代码,这样我才能获得每个工作室的最后制作的电影,而不仅仅是一个工作室?

Try: 尝试:

SELECT a.NAME,
       a.TITLE,
       a.YEAR
FROM (select s.NAME, 
       m.TITLE, 
       m.YEAR,
       ROW_NUMBER()OVER(PARTITION BY s.NAME ORDER BY m.YEAR DESC ) as rnk
from STUDIO s
join MOVIE m on s.NAME = m.STUDIONAME) a
WHERE a.rnk = 1
SELECT *
FROM (
SELECT STUDIO.NAME, MOVIE.TITLE, MOVIE.YEAR
from 
    STUDIO 
join 
    MOVIE on STUDIO.NAME = MOVIE.STUDIONAME
ORDER BY MOVIE.YEAR DESC
) AS newTable
GROUP BY newTable.NAME

The answer above is fine for finding the most recent year a studio published a movie, however if you need the title too, using that method you'll need to use some form of subselect to get the largest year (and so the most recent) by studio name, and then bring in the title based on this. 上面的答案很适合查找工作室最近发行电影的年份,但是,如果您也需要标题,则使用该方法需要使用某种形式的子选择来获得最大的年份(因此也是最近的年份)按工作室名称命名,然后根据此名称输入标题。

The below does both in a fairly succinct fashion using a CROSS APPLY: 下面使用“ CROSS APPLY”以相当简洁的方式完成了这两项工作:

SELECT 
S.NAME,
MM.TITLE
MM.YEAR
FROM STUDIO S
CROSS APPLY 
    (SELECT TOP 1 TITLE, YEAR FROM MOVIE M
     WHERE M.STUDIONAME = S.STUDIO
     ORDER BY M.YEAR DESC) MM

1st Edit: Added context 2nd Edit: Added edit notes. 第一次编辑:添加了上下文。二次编辑:添加了编辑注释。

You might try something like 您可以尝试类似

select 
    studio.name, 
    movie.title, 
    movie.year
from 
    studio 

    inner join movie on 
      studio.name = movie.studioname

    inner join 
    (
        select
          movie.studioname
          max(movie.year) year
        from
          movie
    ) t1 on
      t1.studioname = studio.studioname and
      t1.year = movie.year

Note that there are limitations to the way you have written your query: 请注意,您编写查询的方式受到限制:

  • There could be multiple movies in the same year - which one is the latest? 同一年可能有多部电影-哪一部是最新的? You would need a date column for that or something that that affect 您可能需要一个日期列或可能影响日期的列
  • Also you are using varchar/chars to join on which is really bad practice since you have to do a string comparison on each match. 另外,您正在使用varchar / chars进行连接,这实际上是不好的做法,因为您必须在每个匹配项上进行字符串比较。 It would be better to use id - integers - which give better performance 最好使用id-整数-以获得更好的性能

What do you mean "not only by one"? 您的意思是“不仅一个人”?

Normally you do such queries using aggregate functions: 通常,您可以使用聚合函数进行此类查询:

select STUDIO.NAME, MOVIE.TITLE, MAX( MOVIE.YEAR )
from STUDIO 
join MOVIE on STUDIO.NAME = MOVIE.STUDIONAME
Group by STUDIO.NAME, MOVIE.TITLE

Is this what you need? 这是您需要的吗?

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

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