sql - Count how many percent of values on each column are nulls -
is there way, through information_schema
or otherwise, calculate how many percent of each column of table (or set of tables, better yet) null
s?
your query has number of problems, importantly not escaping identifiers (which lead exceptions @ best or sql injection attacks in worst case) , not taking schema account. use instead:
select 'select ' || string_agg(concat('round(100 - 100 * count(', col , ') / count(*)::numeric, 2) ', col_pct), e'\n , ') || e'\nfrom ' || tbl ( select quote_ident(table_schema) || '.' || quote_ident(table_name) tbl , quote_ident(column_name) col , quote_ident(column_name || '_pct') col_pct information_schema.columns table_name = 'my_table_name' order ordinal_position ) sub group tbl;
produces query like:
select round(100 - 100 * count(id) / count(*)::numeric, 2) id_pct , round(100 - 100 * count(day) / count(*)::numeric, 2) day_pct , round(100 - 100 * count("odd x") / count(*)::numeric, 2) "odd x_pct" public.my_table_name;
closely related answer on dba.se lot more details:
Comments
Post a Comment