Skip to content
Advertisement

MS Access SQL: Sum IIF for multiple Criteria, Format as Column instead of Row

I’m working in MS Access 365 and I have a table containing multiple status that I want to summarize in a Pie Chart.

Sample Data:

Sales Order# Status
1 New
2 Pending
3 Closed

The Statuses are defined by human readable text in a separate table, but are linked in the master table before I run the query. I have the following query to create a summary of each status in the Table.

SELECT 
     Sum(IIf(Table.Status=1,1,0)) AS ["New"], 
     Sum(IIf(Table.Status=2,1,0)) AS ["Pending"], 
     Sum(IIf(Table.Status=3,1,0)) AS ["Closed"], 
FROM Table;

The issue is that this formats the query result as:

New Pending Closed
1 1 1

And due to the insane rules around MS Access charting, a Pie chart bases the data off of the 1st two columns and not anything else. How can I reformat my results to be in a column format to make the charting work? Thank you.

Advertisement

Answer

I believe that is the hard way. Why not count by grouping?

SELECT status, count(status) 
  FROM Table
 GROUP BY status;

I don’t have MS Access on my Android phone, so I can’t show the output.

The previous query gives counts on the ID, for the names, since you mentioned StatusName could be:

SELECT StatusName, count(StatusName) 
  FROM Table
 GROUP BY StatusName;

That assumes a join is not needed to some other Table to resolve the counts.

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