SQL前10个结果是嵌套查询

时间:2018-04-06 11:21:43

标签: sql nested

希望有人可以帮我制作嵌套查询。不必高效,只需简单易懂。

我期待"找到播放曲目数量最多的前10张专辑"

我有以下表格:  相册,其中包含GRid和标题   AlbumTrack ,其中包含GRid和ISRC  跟踪,其中包含ISRC和PlayCount

我目前得到了:

SELECT TOP 10 Album.Title
FROM Album
WHERE Grid IN

(SELECT AlbumTrack.GRid 
FROM AlbumTrack
WHERE ISRC IN

(SELECT Track,ISRC
FROM TRACK
WHERE Track.ISRC = SUM(Track.PlayCount)
ORDER BY Track.PlayCount));

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

使用加入

SELECT Title 
FROM (
        SELECT Album.Title,
               DENSE_RANK() OVER (ORDER BY PlayCount) AS RN
        FROM Album 
        INNER JOIN AlbumTrack
        ON Album.Grid = AlbumTrack.Grid
        INNER JOIN
        (   SELECT ISRC, SUM(PlayCount) AS PlayCount
            FROM TRACK
            GROUP BY ISRC
        )AS TRACK
        ON AlbumTrack.ISRC=TRACK.ISRC
    ) AS T
WHERE RN<=10

答案 1 :(得分:0)

尝试此查询:

 SELECT TOP 10 Album.Title FROM Album
    JOIN AlbumTrack ON Album.Grid = AlbumTrack.GRid
    JOIN (SELECT Sum(PlayCount) as NumberOfPlay , ISRC  TRACK
    GROUP BY AlbumTrack.ISRC
    ) as Track ON AlbumTrack.ISRC = Track.ISRC
    ORDER BY Track.NumberOfPlay