Search code examples
sqloracle-databaseunpivot

unpivot for all columns which is having null


I want all columns from table with count of null values in each column with null count.

I have query like

Select count(*)-count(COLA),count(*)-count(COLB),......count(*)-count(COLN) FROM TABLE;

I want count for each column from table having null values in row to column.

COLUMN NAME - COUNT
COLA        - 0
COLB        - 2
.
.
.
COLN        - 7

Solution

  • You can write like this:

    Select * from
    (
     Select count(*)-count(COLA) Cola_cnt,count(*)-count(COLB) colb_cnt,......count(*)-count(COLN)
    From your_table
    )
    UNPIVOT
    ( YOUR_COUNT
      FOR col_name in
      (Cola_cnt, colb_cnt, ...)
    );