0

I have a simple MYSQL table with about 5 columns or so. The row size of the table changes quite frequently.

One of these columns is named has_error, and is a column that has a value of either 1 or 0.

I want to create a single SQL query that will be the equivalent of the following simple equation:

(Number of rows with has_error = 1/Total number of rows in table) * 100

I can create the individual SQL queries (see below), but not sure how to put it all together.

SELECT COUNT(*) AS total_number_of_rows FROM my_table
SELECT COUNT(*) AS number_of_rows_with_errors FROM My_table WHERE has_error = 1

1 Answer 1

1

This is easy because you can just use avg(has_error):

SELECT AVG(has_error) * 100
FROM My_table;
Sign up to request clarification or add additional context in comments.

1 Comment

Doh! Not sure why I didn't think of that. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.