Search code examples
sqlsqliteselectgroup-by

select list of albums of artist and display for every album tracks count which have requested artist and album in list


I need to make complicated query. I have table:

CREATE TABLE IF NOT EXISTS tracks
     ( id INTEGER PRIMARY KEY, path TEXT,
     title TEXT, artist TEXT,
     album TEXT, genre TEXT, 
    duration INTEGER, rating INTEGER );

Sory for dirty title of question but i don't understand how to explain it more shortly.

Question: How to display list of albums of requested artist and for each displayed album calculate count of tracks which have requested artist in current album in single query.

I have tried something like this:

SELECT albtbl.album as album, albtbl.artist as artist, 
( SELECT count(*) FROM trackstbl WHERE trackstbl.artist = albtbl.aritst ) as tracksCount 
FROM ( SELECT tbl.album as album, tbl.artist as artist, count(title) as tracksCount FROM tracks as tbl  WHERE tbl.artist = 'requested_artist'
 GROUP BY tbl.album ) as albtbl, FROM tracks as trackstbl ORDER BY album ASC

but it not compiled:

SQLiteException: near "FROM": syntax error:


Solution

  • Not sure, why this is complex. Below query should return you the count of track for each album of searches artist:

         select artist, album, count(*) as tracksCount
         from tracks
         where artist = 'requested_artist'
         group by artist, album;