How to group by and count based on a condition

user2756639

I am new Access and I framing a query to group a table based on a field and get a count of records based on a condition.

Table:

Category       Status
Pen            Open
Pencil         Open
Pen            Closed
Pencil         Closed

I am looking for a query which gives me an output as shown below:

Category      Open       Closed
Pen           1          1
Pencil        1          1 

So far I tried, SELECT Category, COUNT(Status='Open'), Count(Status='Closed') FROM table GROUP BY Category; This query does not solve the issue.

TIA

jpw

One way to do this is to do conditional aggregation using the sum and iif functions:

select 
  category, 
  sum(iif(status='Open',1,0)) as Open,
  sum(iif(status='Closed',1,0)) as Closed,
from table
group by category

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Condition based on COUNT(*) in query with GROUP BY clause

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

How I can count with GROUP BY and condition in MySQL

From Dev

mongo group and count with condition

From Dev

Column count based on a condition

From Dev

Count rows based on a condition

From Dev

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

From Dev

How to add specific condition inside COUNT() in sql while doing a group by?

From Dev

How to count the occurrences of a value in a row based on a WHERE condition?

From Dev

how to count percentage based on several condition mysql from different table

From Dev

how to get the count of column data based on some condition

From Dev

Count each condition within group

From Dev

COUNT with a condition for a query with Group by and Join

From Dev

How to add a condition in GROUP BY based on the number of rows with a certain column value?

From Dev

How to group values of one column based on condition in t-sql?

From Dev

how group the result based on some condition into concatenated results

From Dev

How to find rows based on a condition within a group in sql?

From Dev

How to group by month based on date using java and calculate total count?

From Dev

How to group based on the count of a string attribute equaling a value?

From Dev

How to count events based on parsing date - GROUP BY issues

From Dev

Count from a Count based on a condition in SQL server

From Dev

Count from a Count based on a condition in SQL server

From Dev

Delete pandas group based on condition

From Dev

Mysql group and sum based on condition

From Dev

Get Max in a Group based on a condition

From Dev

Assigning group number based on condition

From Dev

Get Max in a Group based on a condition

From Dev

Delete pandas group based on condition

Related Related

  1. 1

    Condition based on COUNT(*) in query with GROUP BY clause

  2. 2

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

  3. 3

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

  4. 4

    How I can count with GROUP BY and condition in MySQL

  5. 5

    mongo group and count with condition

  6. 6

    Column count based on a condition

  7. 7

    Count rows based on a condition

  8. 8

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

  9. 9

    How to add specific condition inside COUNT() in sql while doing a group by?

  10. 10

    How to count the occurrences of a value in a row based on a WHERE condition?

  11. 11

    how to count percentage based on several condition mysql from different table

  12. 12

    how to get the count of column data based on some condition

  13. 13

    Count each condition within group

  14. 14

    COUNT with a condition for a query with Group by and Join

  15. 15

    How to add a condition in GROUP BY based on the number of rows with a certain column value?

  16. 16

    How to group values of one column based on condition in t-sql?

  17. 17

    how group the result based on some condition into concatenated results

  18. 18

    How to find rows based on a condition within a group in sql?

  19. 19

    How to group by month based on date using java and calculate total count?

  20. 20

    How to group based on the count of a string attribute equaling a value?

  21. 21

    How to count events based on parsing date - GROUP BY issues

  22. 22

    Count from a Count based on a condition in SQL server

  23. 23

    Count from a Count based on a condition in SQL server

  24. 24

    Delete pandas group based on condition

  25. 25

    Mysql group and sum based on condition

  26. 26

    Get Max in a Group based on a condition

  27. 27

    Assigning group number based on condition

  28. 28

    Get Max in a Group based on a condition

  29. 29

    Delete pandas group based on condition

HotTag

Archive