Можно так, например
SELECT a1.id, a1.name
FROM users AS a1 LEFT JOIN users AS a2
ON a1.name = a2.name AND a1.id <= a2.id
GROUP BY a1.id, a1.name
HAVING
(a1.name = 'dasha' AND COUNT(*) <=3)
OR
(a1.name = 'masha' AND COUNT(*) <=5)
ORDER BY a1.name;
(EXPLAIN (analyze) without indexs, total runtime: 46.380 ms)
или так
SELECT * FROM (
SELECT a1.id, a1.name, count(*) AS n
FROM users AS a1 LEFT JOIN users AS a2
ON a1.name = a2.name AND a1.id <= a2.id GROUP BY a1.id, a1.name
) AS t
WHERE (t.name = 'dasha-1' and t.n <= 3) OR (t.name = 'dasha-2' and t.n <= 5)
ORDER BY t.name;
(EXPLAIN (analyze) without indexes, total runtime: 38.711 ms)
(SELECT ...) UNION (...)
(EXPLAIN (analyze) without indexes, total runtime: 1.546 ms)