I am trying to count two columns with 2nd column as distinct values. The 2nd column data can have multiple values in the first column. So i would like to count the 1st column and distinct count of second column and divide the 1st by the 2nd column to get the o/p. Now there is a third column on which we need to group the data.
Example:
A B C
----------------
30 10 tomatoes
30 10 tomatoes
5 10 tomatoes
20 5 Potatoes
20 5 Potatoes
40 5 Potatoes
10 15 Onions
40 15 Onions
20 15 Onions
Looking for possible solutions.
Below is a simple try. I am not sure if this is right or should i use partition by. Any help would be appreciated.
SELECT
C,
COUNT('A') AS A,
COUNT(DISTINCT 'B') AS B,
((COUNT('A')) / COUNT(DISTINCT 'B')) AS AB
FROM
[Table]
GROUP BY
C
ORDER BY
C
COUNT(DISTINCT 'B')will always return1, tryCOUNT(DISTINCT B)