When the table is large and the collection is more unpredictable, line numbering should be sorted by type in the internal query for processing side variables.
select id, title, type from (select id, title, type, @r := CASE WHEN @g = type THEN @r+1 ELSE 1 END r, @g := type from tbl order by type, title) as x where row_number <= 3
Another way to do this without using side effects if the two entries do not match in type (name, type, identifier) ββis given below. This uses only standard ANSI SQL92 SQL. This may be slower than indicated above.
select A.id, A.title, A.type from tbl A left join tbl B on A.title = B.title and (A.type < B.type or (A.type = B.type and A.id < A.id)) group by A.id, A.title, A.type having count(B.title) <= 2
RichardTheKiwi
source share