Skip to content
Advertisement

How to write a SQL to count total number of occurrences of value in column after group by while taking count as 1 if the group has the value?

I have a data with following structure

As title suggests, I want to count occurrence of ‘FAIL’ in ColumnC after grouping by ColumnA, and ColumnB and while counting the occurrence, I want to count only one ‘FAIL’ in the group.

For example, if I did the counting for the above example data, the result will be:

SQL I’ve made so far:

Above query counts every single ‘FAIL’ (above example gives 4 for 1 in ColumnA) in the group but I only want to count only one occurrence in the group. How should I tackle this?

In pandas, I could do something like change value by adding sequence to it by using cumcount and change the values by adding sequence number then count only ‘FAIL1’ after a GROUP BY.

Is it possible to do it in SQL?

Advertisement

Answer

This answers the original version of the question.

I think you want count(distinct):

Or more concisely:

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