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

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

    Another MySQL method is to use the CASE operator, which can be generalised to more alternatives than IF():

    SELECT CASE WHEN processed_timestamp IS NULL THEN 'NULL' 
                ELSE 'NOT NULL' END AS a,
           COUNT(*) AS n 
           FROM logs 
           GROUP BY a
    
    0 讨论(0)
  • 2021-01-30 16:38

    In MySQL you could do something like

    SELECT 
        IF(ISNULL(processed_timestamp), 'NULL', 'NOT NULL') as myfield, 
        COUNT(*) 
    FROM mytable 
    GROUP BY myfield
    
    0 讨论(0)
  • 2021-01-30 16:40
    Select Sum(Case When processed_timestamp IS NULL
                             Then 1
                             Else 0
                     End)                                                               not_processed_count,
              Sum(Case When processed_timestamp Is Not NULL
                             Then 1
                             Else 0
                     End)                                                               processed_count,
              Count(1)                                                                total
    From table
    

    Edit: didn't read carefully, this one returns a single row.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-30 16:44

    If your database has an efficient COUNT(*) function for a table, you could COUNT whichever is the smaller number, and subtract.

    0 讨论(0)
  • 2021-01-30 16:46

    Another way in T-sql (sql-server)

    select  count(case when t.timestamps is null 
                        then 1 
                        else null end) NULLROWS,
            count(case when t.timestamps is not null 
                        then 1 
                        else null end) NOTNULLROWS
    from myTable t 
    
    0 讨论(0)
提交回复
热议问题