How do I return my records grouped by NULL and NOT NULL?

前端 未结 14 1068
说谎
说谎 2021-01-30 16:24

I have a table that has a processed_timestamp column -- if a record has been processed then that field contains the datetime it was processed, otherwise it is null.

14条回答
  •  青春惊慌失措
    2021-01-30 16:29

    In Oracle

    SELECT COUNT(*), COUNT(TIME_STAMP_COLUMN)
    FROM TABLE;
    

    count(*) returns the count of all rows

    count(column_name) returns the number of rows which are not NULL, so

    SELECT COUNT(*) - COUNT(TIME_STAMP_COLUMN) NUL_COUNT,
                      COUNT(TIME_STAMP_COLUMN) NON_NUL_COUNT
    FROM TABLE
    

    ought to do the job.

    If the column is indexed, you might end up with some sort of range scan and avoid actually reading the table.

提交回复
热议问题