2016-05-20 30 views
1

私はそれに関連するすべての詳細を含むトラックの説明を作ろうとしています。同じセル内に複数の値があります

SELECT 
(SELECT 
UPPER(Track.Name) || ' is a ' || 
CAST(Track.Milliseconds/1000 AS INT) || ' seconds long track in the album ' || 
UPPER(Album.Title) || ' of ' || 
Artist.Name || ' composed by ' || 
CASE WHEN Track.Composer IS NULL THEN 'an unknown composer' ELSE Track.Composer END || 
'. ' || 'It is available as a ' || 
MediaType.Name || ' for $' || 
Track.UnitPrice || ', and it can be found in the following playlists: ' || 
Playlist.Name) 
AS 'Track Description' FROM Track 
LEFT JOIN Album 
ON Track.AlbumId=Album.AlbumId 
INNER JOIN Artist 
ON Artist.ArtistId=Album.ArtistId 
INNER JOIN MediaType 
ON Track.MediaTypeId=MediaType.MediaTypeId 
INNER JOIN PlaylistTrack 
ON PlaylistTrack.TrackId = Track.TrackId 
INNER JOIN Playlist 
ON Playlist.PlaylistId = PlaylistTrack.PlaylistId 
ORDER BY RANDOM() 
LIMIT 1; 

私はplaylist.nameと協力しています。 1つのトラックは複数のプレイリストに存在することができ、現在はトッププレイリストのみを出力します。 and it can be found in the following playlists: ' ||

答えて

0

GROUP_CONCATを使用すると、特定のトラックが表示されるすべてのプレイリスト名を集約することができます。これを行うには、GROUP BYのトラックに関連付けられたすべての列、再生リスト名の場合はを除くことができます。サブクエリは、内側のクエリからかなりの形式で出力を計算します。

SELECT UPPER(t.trackName) || ' is a ' || 
    CAST(t.trackMillis/1000 AS INT) || ' seconds long track in the album ' || 
    UPPER(t.albumTitle) || ' of ' || 
    t.artistName || ' composed by ' || 
    CASE WHEN t.trackComposer IS NULL THEN 'an unknown composer' ELSE t.trackComposer END || 
    '. ' || 'It is available as a ' || 
    t.mediaTypeName || ' for $' || 
    t.trackUnitPrice || ', and it can be found in the following playlists: ' || t.playlistNames 
FROM 
(
    SELECT Track.Name AS trackName, Track.Milliseconds AS trackMillis, 
     Album.Title AS albumTitle, Artist.Name AS artistName, 
     Track.Composer AS trackComposer, MediaType.Name AS mediaTypeName, 
     Track.UnitPrice AS trackUnitPrice, GROUP_CONCAT(Playlist.Name) AS playlistNames 
    FROM Track 
    LEFT JOIN Album 
     ON Track.AlbumId = Album.AlbumId 
    INNER JOIN Artist 
     ON Artist.ArtistId = Album.ArtistId 
    INNER JOIN MediaType 
     ON Track.MediaTypeId = MediaType.MediaTypeId 
    INNER JOIN PlaylistTrack 
     ON PlaylistTrack.TrackId = Track.TrackId 
    INNER JOIN Playlist 
     ON Playlist.PlaylistId = PlaylistTrack.PlaylistId 
    GROUP BY Track.Name, Track.Milliseconds, Album.Title, Artist.Name, 
     Track.Composer, MediaType.Name, Track.UnitPrice 
) t 
関連する問題