Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get percent of columns that completed by calculating null values

Tags:

sql

mysql

I have a table with a column that allows nulls. If the value is null it is incomplete. I want to calculate the percentage complete.

Can this be done in MySQL through SQL or should I get the total entries and the total null entries and calculate the percentage on the server?

Either way, I'm very confused on how I need to go about separating the variable_value so that I can get its total results and also its total NULL results.

SELECT
    games.id
FROM 
    games
WHERE 
    games.category_id='10' AND games.variable_value IS NULL

This gives me all the games where the variable_value is NULL. How do I extend this to also get me either the TOTAL games or games NOT NULL along with it?

Table Schema:

id (INT Primary Auto-Inc)

category_id (INT)

variable_value (TEXT Allow Null Default: NULL)

like image 649
krx Avatar asked Jul 26 '10 19:07

krx


People also ask

How do you find the percentage of NULL in a DataFrame?

To find the percentage of missing values in each column of an R data frame, we can use colMeans function with is.na function. This will find the mean of missing values in each column. After that we can multiply the output with 100 to get the percentage.


1 Answers

SELECT
    SUM(CASE WHEN G.variable_value IS NOT NULL THEN 1 ELSE 0 END)/COUNT(*) AS pct_complete
FROM
    Games G
WHERE
    G.category_id = '10'

You might need to do some casting on the SUM() so that you get a decimal.

like image 189
Tom H Avatar answered Oct 18 '22 22:10

Tom H