终极能量
有时,您可能希望"保存"查询结果,便于后续继续处理数据。您可以通过创建一个临时表来实现,它会一直保留在数据库中,直到 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;