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

前端 未结 14 1194
说谎
说谎 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:26

    Try the following, it's vendor-neutral:

    select
        'null    ' as type,
        count(*)   as quant
        from       tbl
        where      tmstmp is null
    union all
    select
        'not null' as type,
        count(*)   as quant
        from       tbl
        where      tmstmp is not null
    

    After having our local DB2 guru look at this, he concurs: none of the solutions presented to date (including this one) can avoid a full table scan (of the table if timestamp is not indexed, or of the indexotherwise). They all scan every record in the table exactly once.

    All the CASE/IF/NVL2() solutions do a null-to-string conversion for each row, introducing unnecessary load on the DBMS. This solution does not have that problem.

提交回复
热议问题