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?

Jeeva Bharathi

I have a data with following structure

ColummnA ColumnB ColumnC

  1         1      Test
  1         1      FAIL
  1         1      FAIL

  1         2      FAIL
  1         2      FAIL
  1         2      FAIL

  2         1      TEST
  2         1      FAIL

As title suggests, I want to count occurrence of 'FAIL' in ColumnC after grouping by ColumnA, and ColumnB and while counting the occurence, 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:

ColumnA ColumnB count
   1      1       1(not 2)
   1      2       1(not 3)
   2      1       1

SQL I've made so far:

SELECT
sum((CASE ColumnC WHEN 'FAIL' THEN 1 WHEN 'TEST' THEN 0 WHEN 'Test' THEN 0 END))
FROM
table
GROUP BY ColumnA, ColumnB

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?

Gordon Linoff

This answers the original version of the question.

I think you want count(distinct):

select columnA,
       count(distinct case when columnC = 'Fail' then columnB end)
from t
group by columnA;

Or more concisely:

select columnA, count(distinct columnB)
from t
where columnC = 'Fail'
group by columnA;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Count number of occurrences of a value in MYSQL and group by ID

Group By and Count the total number of same status value

How to delete the row that has least value in a specific column if group by count of the column is greater than 1

How to count occurrences of a column value efficiently in SQL?

SQL GROUP BY and COUNT and conditional SUM with column value

Count the occurrences of a group of words in a text column in SQL

How do I group by the count value in sql

Count number of times value has appeared in last x rows in group

How to count the number of occurrences of a specific value in a column in SQL without doing two SQL calls

Group By on Column A and calculate value count on column B in SQL

KDB/q count number of rows group by value of a column

count number of rows before a value group by another column in pandas dataframe

Count occurrences of a column value efficiently in SQL?

count sum of a column and group it by column with same value

SQL to count rows in a group where a column is equal to a particular value

SQL count rows with same value in column and group by id?

How to count number of occurrences of a value in a map with Golang?

How to get a COUNT() value when GROUP BY has no row?

Android Firebase - Count the total number of occurrences of a value within a list

Excel Formula || How to count occurrences of a value in column

Count how many occurrences of value in a column

How to count occurrences of a distinct value in a column?

Count number of categories after GROUP BY in SQL or Pandas

(R) Bin a numeric column to count occurrences after group by

Count number of sequences of a specific value per group

Total Count of column value in Sql Server

How do I count the number of occurences of a specific value in a group?

Count of Value column with case when and group by

Mysql count occurrence by group as a column value