Suppose you have a table with the content below:
------------------- | ID | NAME | GROUP | +-------------------+ | 1 | A | 1 | +-------------------+ | 2 | B | 2 | +-------------------+ | 3 | C | 2 | +-------------------+ | 4 | D | 3 | +-------------------+ | 5 | E | 1 | +-------------------+ | 6 | F | 3 | +-------------------+
The following self LEFT JOIN counts the number of different values ββin a GROUP.
SELECT COUNT(*) FROM table AS t1 LEFT JOIN table AS t2 ON t2.GROUP = t1.GROUP AND t2.ID > t1.ID WHERE t2.id IS NULL;
What this query does is find out for each group the item with the highest identifier.
pedromanoel
source share