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

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

    Stewart,

    Maybe consider this solution. It is (also!) vendor non-specific.

    SELECT count([processed_timestamp]) AS notnullrows, 
           count(*) - count([processed_timestamp]) AS nullrows 
    FROM table
    

    As for efficiency, this avoids 2x index seeks/table scans/whatever by including the results on one row. If you absolutely require 2 rows in the result, two passes over the set may be unavoidable because of unioning aggregates.

    Hope this helps

提交回复
热议问题