为播放列表表格中的播放列表选择最常用的曲目

时间:2017-03-26 06:07:08

标签: mysql sql

我正在使用以下字段创建包含所有播放列表统计信息的摘要表:

PlaylistName LatestTrackAddDate NumberOfTracks TracksPlayedCount NumberOfArtists MostPopularSong

我可以使用以下查询获取除第一个以外的所有字段:

SELECT p.playlist_name AS 'Playlist Name', 
    MAX(plt.playlisttrack_adddate) 'Latest Track Add Date', 
    COUNT(DISTINCT plt.playlist_id, plt.track_id, plt.user_id, plt.playlisttrack_adddate) As 'Number of Tracks', 
    COUNT(DISTINCT pt.user_id, pt.track_id, playedtrack_datetime) As 'Number of Tracks Played',
    COUNT(DISTINCT a.artist_id) As 'Number of Unique Artists'
FROM playlist p
    JOIN playlisttrack plt ON p.playlist_id = plt.playlist_id
    JOIN tracksource ts ON ts.tracksource_typeid = p.playlist_id
    JOIN playedtrack pt ON pt.tracksource_id = ts.tracksource_id
    JOIN track t ON plt.track_id = t.track_id
        AND pt.track_id = t.track_id
    JOIN trackartist ta ON t.track_id = ta.track_id
    JOIN artist a ON ta.artist_id = a.artist_id
GROUP BY p.playlist_name;

我可以使用以下内容在自己的查询中获取最后一个字段:

SELECT playlist_name, track_name, MAX(count_plays)
FROM(
    SELECT p.playlist_name AS playlist_name, 
        t.track_name AS track_name,
        COUNT(pt.playedtrack_datetime)  AS count_plays
    FROM playlist p
        JOIN playlisttrack plt ON p.playlist_id = plt.playlist_id
        JOIN tracksource ts ON ts.tracksource_typeid = p.playlist_id
        JOIN playedtrack pt ON pt.tracksource_id = ts.tracksource_id
        JOIN track t ON plt.track_id = t.track_id
            AND pt.track_id = t.track_id
        JOIN trackartist ta ON t.track_id = ta.track_id
        JOIN artist a ON ta.artist_id = a.artist_id
    GROUP BY p.playlist_name, t.track_name) combined
GROUP BY playlist_name;

我不太确定如何将这两个结果结合起来 - 我们非常感谢任何指导!谢谢!

编辑: 如果有多个曲目具有最大播放次数,则它们都会出现在表格中。

这些查询中使用的表的结构(其他被省略): Partial snippet of schema

1 个答案:

答案 0 :(得分:0)

请尝试 -

SELECT nameOfPlaylist AS "Playlist Name",
       dateTrackLastAdded AS "Latest Track Add Date",
       trackCount AS "Number Of Tracks",
       numberOfTracksPlayed AS "Number Of Tracks Played",
       uniqueArtistCount AS "Number Of Unique Artists",
       playedTrackID AS "Most Popular Song(s)"
FROM
(
    SELECT playlist.playlist_id AS playlist_id,
           playlist.playlist_name AS nameOfPlaylist,
           MAX( playlisttrack.playlisttrack_adddate ) AS dateTrackLastAdded, 
           COUNT( playlisttrack.track_id ) AS trackCount,
           COUNT( DISTINCT playedtrack.track_id ) AS numberOfTracksPlayed,
           COUNT( DISTINCT trackartist.artist_id ) AS uniqueArtistCount,
           playedtrack.track_id AS playedTrackID,
           COUNT( playedtrack.track_id ) AS distributedTrackPlayCount
    FROM playlist
    JOIN playlisttrack ON playlist.playlist_id = playlisttrack.playlist_id
    JOIN tracksource ON tracksource.tracksource_typeid = playlist.playlist_id
    JOIN playedtrack ON playedtrack.tracksource_id = tracksource.tracksource_id
    JOIN track ON playlisttrack.track_id = track.track_id AND playedTrackID = track.track_id
    JOIN trackartist ON track.track_id = trackartist.track_id
    GROUP BY playlist.playlist_name,
             playedTrackID
) withDistributedTrackPlayCount
JOIN
(
    SELECT withConsolidatedTrackCount.playlist_id,
           MAX( consolidatedTrackPlayCount ) AS maxPlayCount
    FROM
    (
        SELECT withDistributedTrackPlayCount.playlist_id AS playlist_id,
               withDistributedTrackPlayCount.playedTrackID AS playedTrackID,
               SUM( distributedTrackPlayCount ) AS consolidatedTrackPlayCount
        FROM withDistributedTrackPlayCount
        GROUP BY withDistributedTrackPlayCount.playlist_id
    ) withConsolidatedTrackCount
) withMaxTrackPlayCount ON withDistributedTrackPlayCount.playlist_id = withMaxTrackPlayCount.playlist_id;

注意:我无法测试这个,因为我没有一个适当的等效数据库,而是必须构建一个。如果您有构建和填充上述内容的脚本,请修改您的问题以包含它们。

注意:我假设“播放的曲目数量”是指属于播放列表的曲目的计数,这些曲目至少一次作为该播放列表的一部分播放。如果它指的是属于播放列表的曲目已经作为播放列表的一部分播放的总次数,那么请说出来,我将修改我的答案。

注意:您已声明可以由多个用户将一首曲目添加到播放列表,每个用户可能会多次添加该曲目。我假设“最受欢迎的歌曲”是指播放列表中所有列表中的曲目播放总数。

我欢迎所有与此答案相关的建设性问题或评论。