Skip to content
Advertisement

SQL – get distinct values and its frequency count of occurring in a group

I have a table data as:

The data set is as follows:

I need to list all distinct search_product_result values and count frequencies of these values occurring in s_product_id.

Required Output result-set:

Here, A occurs in three s_product_id : 0, 1, 2, B in two : 0, 2, and so on.

D occurred twice in the same group 3, but is counted only once for that group.

I tried grouping by search_product_result, but this counts D twice in the same group.

Output:

Advertisement

Answer

You can try below – use count(distinct s_product_id)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement