Filter data based on result set of group and count

theking963

I have the following table

Col1    Col2    Col3
A1      B1      C1
A1      B1      C2
A1      B2      C1
A1      B2      C2
A1      B2      C3
A2      B1      C1
A2      B1      C2
A2      B2      C1
A2      B2      C2

From this table I want all the unique records from Col1 where for the combination of col1 and col2 there's a different count for the same value in Col1. The only possible answer is A1 in the table above.

The following query gives me the count of each col1 and col2.

select col1, col2, count(*) from table
group by col1, col2;

Col1    Col2    Count
A1      B1      2
A1      B2      3
A2      B1      2
A2      B2      2

From the above query I can see that A1 has two records with a different count. How do I return A1 in a single query?

Gordon Linoff

You can use another level of aggregation:

select col1
from (select col1, col2, count(*) as cnt
      from table
      group by col1, col2
     ) t
group by col1
having min(cnt) <> max(cnt);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the count of a group based on another group and plot the result

From Dev

Get the count of data based on id in mysql result

From Dev

Group and count data based on a calculated field in mysql?

From Dev

Filter data based on group SQL Netezza

From Dev

Filter data based on group SQL Netezza

From Java

How to insert query filter based on count result in redash

From Dev

mysql - filter query result based on count of one column's value?

From Dev

SELECT COUNT() GROUP BY Set of data in IN() mysql

From Dev

SELECT COUNT() GROUP BY Set of data in IN() mysql

From Dev

Filter group by result in DataFrame

From Dev

MySQL filter by GROUP BY result

From Dev

Group Observables and then filter by count

From Dev

Filter data.frame based on rowwise NA count

From Java

using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

From Dev

using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

From Dev

count number of rows in a data frame in R based on group

From Java

Filter a group of a data.frame based on multiple conditions

From Dev

Postgres count unique result in GROUP BY

From Dev

PostgreSQL merge COUNT and GROUP BY result

From Dev

Inserting extra rows into a result set based on the data length of a text column

From Dev

How to update columns data based on previous rows of a result set

From Dev

Angular JS Group by Count With Filter

From Dev

group, filter and count in nested array

From Dev

D3: Set filter flood-color based on data

From Dev

Group the Result based on RowCount in Oracle

From Dev

Count within the result set of a subquery

From Dev

Is it possible to filter based on count in Kibana?

From Dev

SQL group partitioned result set

From Dev

how to filter result set based on one column distict values and discard rest using oracle

Related Related

  1. 1

    How to get the count of a group based on another group and plot the result

  2. 2

    Get the count of data based on id in mysql result

  3. 3

    Group and count data based on a calculated field in mysql?

  4. 4

    Filter data based on group SQL Netezza

  5. 5

    Filter data based on group SQL Netezza

  6. 6

    How to insert query filter based on count result in redash

  7. 7

    mysql - filter query result based on count of one column's value?

  8. 8

    SELECT COUNT() GROUP BY Set of data in IN() mysql

  9. 9

    SELECT COUNT() GROUP BY Set of data in IN() mysql

  10. 10

    Filter group by result in DataFrame

  11. 11

    MySQL filter by GROUP BY result

  12. 12

    Group Observables and then filter by count

  13. 13

    Filter data.frame based on rowwise NA count

  14. 14

    using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

  15. 15

    using Java 8 How to filter by list and group by based on filter condition and convert to Map with their count

  16. 16

    count number of rows in a data frame in R based on group

  17. 17

    Filter a group of a data.frame based on multiple conditions

  18. 18

    Postgres count unique result in GROUP BY

  19. 19

    PostgreSQL merge COUNT and GROUP BY result

  20. 20

    Inserting extra rows into a result set based on the data length of a text column

  21. 21

    How to update columns data based on previous rows of a result set

  22. 22

    Angular JS Group by Count With Filter

  23. 23

    group, filter and count in nested array

  24. 24

    D3: Set filter flood-color based on data

  25. 25

    Group the Result based on RowCount in Oracle

  26. 26

    Count within the result set of a subquery

  27. 27

    Is it possible to filter based on count in Kibana?

  28. 28

    SQL group partitioned result set

  29. 29

    how to filter result set based on one column distict values and discard rest using oracle

HotTag

Archive