----------------------------
id|key|value1|value2|value3|
----------------------------
1 |91 |a | |f |
2 |91 | |c | |
3 |95 | |d |g |
4 |95 |b |e | |
SELECT DISTINCT
key,
FIRST_VALUE(value1) OVER (PARTITION BY key
ORDER BY CASE WHEN value1 IS NULL
THEN 1
ELSE 0 END, id DESC) AS value1,
FIRST_VALUE(value2) OVER (PARTITION BY key
ORDER BY CASE WHEN value2 IS NULL
THEN 1
ELSE 0 END, id DESC) AS value2,
FIRST_VALUE(value3) OVER (PARTITION BY key
ORDER BY CASE WHEN value3 IS NULL
THEN 1
ELSE 0 END, id DESC) AS value3
FROM tablename
-- WHERE key IN ( {список значений} )
переписать таблицу в нормальную форму
SELECT `t`.`key`, `t1`.`value1`,
`t2`.`value2`, `t3`.`value3`
FROM (
SELECT `key`,
MAX(IF(`value1` = '', -1, `id`)) AS `id1`,
MAX(IF(`value2` = '', -1, `id`)) AS `id2`,
MAX(IF(`value3` = '', -1, `id`)) AS `id3`
FROM `table`
GROUP BY `key`
) AS `t`
LEFT JOIN `table` AS `t1` ON `t1`.`id` = `t`.`id1`
LEFT JOIN `table` AS `t2` ON `t2`.`id` = `t`.`id2`
LEFT JOIN `table` AS `t3` ON `t3`.`id` = `t`.`id3`