sammarcow
sammarcow

Reputation: 2956

Count on multiple fields

How do I get an output from table of fields A, B, C to display

A | Count(A)

where the number of rows = distinct instances of A and the sum of the values for count(A) equals total number of rows in the database?

Upvotes: 0

Views: 70

Answers (3)

Juzer
Juzer

Reputation: 33

Like AI mentioned a dataset and the final outcome would be helpful. From the small example you gave, i think this might be what you are looking for

SELECT Field_A, Count(*) FROM table_name GROUP BY Field_A UNION ALL SELECT Field_B, Count(*) FROM table_name GROUP BY Field_B UNION ALL SELECT Field_C, Count(*) FROM table_name GROUP BY Field_C

The output for that would be something similiar to depending on how many unique values you have in Field_A, Field_B, Field_C

Value_In_A1 - 10 Value_In_A2 - 11 Value_In_A3 - 15 Value_In_A4 - 8 Value_In_B1 - 9 Value_In_C1 - 25 Value_In_C2 - 2

Upvotes: 0

Dave Sexton
Dave Sexton

Reputation: 11188

You can use the DISTINCT keyword inside the COUNT

Like this:

COUNT(DISTINCT A)

Upvotes: 0

tom
tom

Reputation: 19173

SELECT A, COUNT(1) FROM your_table_name GROUP BY A;

Upvotes: 1

Related Questions