Hide Empty columns

前端 未结 4 649
野性不改
野性不改 2021-01-14 14:07

I got a table with 75 columns,. what is the sql statement to display only the columns with values in in ?

thanks

4条回答
  •  遥遥无期
    2021-01-14 14:15

    It's true that a similar statement doesn't exist (in a SELECT you can use condition filters only for the rows, not for the columns). But you could try to write a (bit tricky) procedure. It must check which are the columns that contains at least one not NULL/empty value, using queries. When you get this list of columns just join them in a string with a comma between each one and compose a query that you can run, returning what you wanted.

    EDIT: I thought about it and I think you can do it with a procedure but under one of these conditions:

    • find a way to retrieve column names dynamically in the procedure, that is the metadata (I never heard about it, but I'm new with procedures)

    • or hardcode all column names (loosing generality)

    You could collect column names inside an array, if stored procedures of your DBMS support arrays (or write the procedure in a programming language like C), and loop on them, making a SELECT each time, checking if it's an empty* column or not. If it contains at least one value concatenate it in a string where column names are comma-separated. Finally you can make your query with only not-empty columns!

    Alternatively to stored procedure you could write a short program (eg in Java) where you can deal with a better flexibility.

    *if you check for NULL values it will be simple, but if you check for empty values you will need to manage with each column data type... another array with data types?

提交回复
热议问题