How can I create a new column that returns the number of distinct values in each row inside my table? For instance,
ID Description Pay1 Pay2 Pay3 #UniquePays
1 asdf1 10 20 10 2
2 asdf2 0 10 20 3
3 asdf3 100 100 100 1
4 asdf4 0 10 3
The query may return >1million rows so it needs to be somewhat efficient. There are 8 'Pay' columns in total, which are either NULL or an integer. Also note that '0' should be counted distinct from NULL.
The most I've been able to accomplish thus far (which I just realized isn't even accurate) is counting the total number of Pay entries in each row:
nvl(length(length(Pay1)),0)
+nvl(length(length(Pay2)),0)
+nvl(length(length(Pay3)),0) "NumPays"
The typical row only has 4 of the 8 columns populated, with the rest being null, and the max integer in the Pay column is '999' (hence the length-length conversion attempt..)
My SQL skills are primitive but any help is appreciated!