CREATE TEMP TABLE CoreCache (
                        OrderID INTEGER PRIMARY KEY,
                        ModelID INTEGER,
                        ItemID TEXT);

DELETE FROM CoreCache WHERE ModelID = 371;
INSERT INTO CoreCache (ModelID, ItemID) SELECT 371, CoreTracks.TrackID FROM CoreTracks CROSS JOIN CoreArtists,CoreAlbums WHERE CoreArtists.ArtistID = CoreTracks.ArtistID AND CoreAlbums.AlbumID = CoreTracks.AlbumID  AND CoreTracks.PrimarySourceID = 1  ORDER BY 
                    CoreAlbums.ArtistNameSortKey ASC,
                    CoreAlbums.TitleSortKey ASC,
                    CoreTracks.Disc ASC,
                    CoreTracks.TrackNumber ASC;

DELETE FROM CoreCache WHERE ModelID = 9;
INSERT INTO CoreCache (ModelID, ItemID) SELECT 9, CoreTracks.TrackID FROM (SELECT MIN(CoreTracks.TrackID) AS TrackID, CoreTracks.Year FROM CoreTracks GROUP BY CoreTracks.Year) AS CoreTracks
                WHERE CoreTracks.Year IN
                    (SELECT CoreTracks.Year FROM CoreTracks, CoreCache
                        WHERE CoreCache.ModelID = 371 AND
                              CoreCache.ItemID = CoreTracks.TrackID )
                    ORDER BY Year;
