0

I imported to a MariaDB table a CSV file generated by this tool with all my last.fm scrobbles, the CREATE script is the following:

CREATE TABLE `scrobbles` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `artist` VARCHAR(128) NULL DEFAULT '',
    `album` VARCHAR(128) NULL DEFAULT '',
    `title` VARCHAR(128) NULL DEFAULT '',
    `datahora` DATETIME NULL DEFAULT current_timestamp(),
    PRIMARY KEY (`id`)
)ENGINE=InnoDB;

I want to know how can i get the most executed tracks (basically the title+artist combo most repeating) of a given year, ordered by the number of plays/scrobbles of each track.

GMB
  • 216,147
  • 25
  • 84
  • 135
Vico
  • 171
  • 1
  • 13

1 Answers1

2

If you want this for a single year, you can aggregate, sort and limit:

select artist, album, title, count(*) cnt
from scrobbles
where datahora >= '2019-01-01' and datahora < '2020-01-01'
group by artist, album, title
order by count(*) desc limit 100

I added the album to the group by clause, as one might expect homonym titles across different albums.

If you want this for multiple years at once, then I would recommend window functions:

select *
from (
    select artist, album, title, year(datahora) yr, count(*) cnt,
        rank() over(partition by year(datahora) order by count(*) desc) rn
    from scrobbles
    group by artist, album, title
) t
where rn <= 100
order by yr, cnt desc

Another benfit of this approach is that it allows bottom ties; it might return more than 100 rows per year if there happened to be ties in the last position.

GMB
  • 216,147
  • 25
  • 84
  • 135
  • 1
    Thanks a lot! I will take your first snipped and remove "album" because grouping "homonym titles" is desired (due to how Last.fm handles albums). As soon i can mark your answer as accepted i will do! – Vico Dec 07 '20 at 23:27