count distinct number of records in sql
Just use a simple and standardized GROUP BY and COUNT(DISTINCT ..)

CREATE TABLE foo(a,b,c)
AS VALUES 
    ( 1, 20, 1 ),
    ( 3, 21, 2 ),
    ( 1, 22, 3 ),    
    ( 2, 22, 4 ),
    ( 3, 21, 5 ),
    ( 1, 20, 6 );

SELECT a, COUNT(DISTINCT b)
FROM foo
GROUP BY a;