Like one request ...
SELECT MIN(id) AS id, name, parent_id, school_id FROM yourTable GROUP BY CASE WHEN name = 'unknown' THEN id ELSE 0 END, name, parent_id, school_id
Or maybe ...
GROUP BY CASE WHEN name <> 'unknown' THEN name ELSE CAST(id AS VARCHAR(???)) END, parent_id, school_id -- Where VARCHAR(???) is the data type of the `name` field. -- Also assumes no value in `name` is the same as an id for an 'unknown' field
Both avoid UNION and the overhead of parsing the table twice, replacing it with the slightly increased GROUP BY
complexity.
MatBailie
source share