I have a table with 300+ columns, many of these columns have no data in them. is there a query I can use to find out the names of these columns so I can remove them for the table. Also I am using a postgresql database on a redshift server if that matters
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- What means in Dart static type and why it differs
- What is the best way to do a search in a large fil
You can try to use jsonb type and functions.
Lets say that your table declared as
First of all lets convert table's rows to jsonb. It is simple:
Result (for test data)
Next we will convert those result to rows like (key,value) using another json function:
Result:
It is almost what we need. Next step:
Result
Here we use
(w)
to specify that it is field instead of table.And the last big step:
Result
Try to use the last query just replacing
t
to your table name.Upd:
Also you can try use PostgreSQL statistics info:
In the
stanullfrac
column you will see the relative amount of nulls for each table's column where 1 means all nuls (but I am not sure how it accurate)First you get the field names
Then using a loop you create a dinamic query
Using a cursor with dynamic SQL in a stored procedure