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

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

    I personally like Pax's solution, but if you absolutely require only one row returned (as I had recently), In MS SQL Server 2005/2008 you can "stack" the two queries using a CTE

    with NullRows (countOf)
    AS
    (
        SELECT count(*) 
        FORM table 
        WHERE [processed_timestamp] IS NOT NULL
    )
    SELECT count(*) AS nulls, countOf
    FROM table, NullRows
    WHERE [processed_timestamp] IS NULL
    GROUP BY countOf
    

    Hope this helps

提交回复
热议问题