count distinct values over columns I struggle to get the result for
here is my dataset




A
B
C
cnt




2
1
2



1
5
5



2
1
4



1
1
1





result expected




A
B
C
cnt




2
1
2
2


1
5
5
2


2
1
4
3


1
1
1
1




PG 9.5
A correlated subquery against a VALUES table should do the trick:
SELECT
    t.A, t.B, t.C,
    (SELECT COUNT(DISTINCT v.n) FROM (VALUES(t.A),(t.B),(t.C)) AS v(n)) AS cnt
FROM table t

If you wanted separate calculations on the values such as both counting and summing, you could use a lateral join (a.k.a APPLY) over VALUES instead:
SELECT A, B, C ,D, E, v.countDistinct, v.countNotNull, v.Total
FROM  test,
LATERAL (
    SELECT COUNT(DISTINCT v.n), COUNT(v.n), SUM(v.n)
    FROM (VALUES (A),(B),(C), (D), (E)) AS v(n)
) AS v(countDistinct, countNotNull, Total)