-- @query memories_thumbnails
-- @db thumbnailCache 9.0
-- @param ids TEXT
-- @with largest
-- @end-with
SELECT tm.driveItemId, MIN(tm.id) AS thumbnailId
FROM thumbnailCache.thumbnail_metadata AS tm
WHERE tm.driveItemId IN (SELECT value FROM json_each(?ids))
-- @with largest
  AND tm.boxSize = (
      SELECT MAX(t2.boxSize)
      FROM thumbnailCache.thumbnail_metadata AS t2
      WHERE t2.driveItemId = tm.driveItemId
  )
-- @end-with
GROUP BY tm.driveItemId
