Home > front end >  Count results by column
Count results by column

Time:12-09

I have a large table with many columns. I need to output the name of each column and the count of how many rows of data, i.e. NOT NULL for each column, something like this:

col_name_1   44
coln_name_2  299
...

Not sure why I'm having a brain freeze. The most I can think of is:

SELECT 
    COUNT(col_name_1), 
    COUNT(col_name_2)
FROM t1
WHERE 
    col_name_1 IS NOT NULL,
    col_name_2 IS NOT NULL

CodePudding user response:

COUNT(col_name) will count the number of rows where col_name is not null.

You can then repeat that for all the columns you're interested in.

SELECT 
    COUNT(col_name_1) AS col_name_1,
    COUNT(col_name_2) AS col_name_2,
    ...
FROM yourTable

You don't need the WHERE clause, because that will only count rows where ALL the columns are not null.

  • Related