Ultimate Power
有時你會想先「儲存」查詢的結果,方便之後繼續處理該資料。你可以建立一個臨時資料表,這個資料表會留在資料庫中,直到 SQL Server 重新啟動為止。在這個最後的練習中,你將從每張專輯挑出最長的曲目,並把結果加入你在查詢中建立的臨時資料表。
本練習屬於課程
SQL Server 入門
練習說明
- 透過
SELECT陳述式,將資料插入名為#maxtracks的臨時資料表。 - 使用
artist_id將album與artist連接,並使用album_id將track與album連接。 - 執行最後的
SELECT陳述式,從你新的資料表擷取所有欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
SELECT album.title AS album_title,
artist.name as artist,
MAX(track.milliseconds / (1000 * 60) % 60 ) AS max_track_length_mins
-- Name the temp table #maxtracks
INTO ___
FROM album
-- Join album to artist using artist_id
INNER JOIN artist ON album.artist_id = artist.artist_id
-- Join track to album using album_id
___
GROUP BY artist.artist_id, album.title, artist.name,album.album_id
-- Run the final SELECT query to retrieve the results from the temporary table
SELECT album_title, artist, max_track_length_mins
FROM #maxtracks
ORDER BY max_track_length_mins DESC, artist;