Counting null and non-null values in a single query

后端 未结 26 1082
星月不相逢
星月不相逢 2021-01-29 19:31

I have a table

create table us
(
 a number
);

Now I have data like:

a
1
2
3
4
null
null
null
8
9

Now I need

相关标签:
26条回答
  • 2021-01-29 19:57

    This works in T-SQL. If you're just counting the number of something and you want to include the nulls, use COALESCE instead of case.

    IF OBJECT_ID('tempdb..#us') IS NOT NULL
        DROP TABLE #us
    
    CREATE TABLE #us
        (
        a INT NULL
        );
    
    INSERT INTO #us VALUES (1),(2),(3),(4),(NULL),(NULL),(NULL),(8),(9)
    
    SELECT * FROM #us
    
    SELECT CASE WHEN a IS NULL THEN 'NULL' ELSE 'NON-NULL' END AS 'NULL?',
            COUNT(CASE WHEN a IS NULL THEN 'NULL' ELSE 'NON-NULL' END) AS 'Count'
        FROM #us
        GROUP BY CASE WHEN a IS NULL THEN 'NULL' ELSE 'NON-NULL' END
    
    SELECT COALESCE(CAST(a AS NVARCHAR),'NULL') AS a,
            COUNT(COALESCE(CAST(a AS NVARCHAR),'NULL')) AS 'Count'
        FROM #us
        GROUP BY COALESCE(CAST(a AS NVARCHAR),'NULL')
    
    0 讨论(0)
  • 2021-01-29 19:57

    for counting not null values

    select count(*) from us where a is not null;
    

    for counting null values

     select count(*) from us where a is null;
    
    0 讨论(0)
  • 2021-01-29 19:58

    Building off of Alberto, I added the rollup.

     SELECT [Narrative] = CASE 
     WHEN [Narrative] IS NULL THEN 'count_total' ELSE    [Narrative] END
    ,[Count]=SUM([Count]) FROM (SELECT COUNT(*) [Count], 'count_nulls' AS [Narrative]  
    FROM [CrmDW].[CRM].[User]  
    WHERE [EmployeeID] IS NULL 
    UNION
    SELECT COUNT(*), 'count_not_nulls ' AS narrative 
    FROM [CrmDW].[CRM].[User] 
    WHERE [EmployeeID] IS NOT NULL) S 
    GROUP BY [Narrative] WITH CUBE;
    
    0 讨论(0)
  • 2021-01-29 19:58
    select count(isnull(NullableColumn,-1))
    
    0 讨论(0)
  • 2021-01-29 19:59

    This is little tricky. Assume the table has just one column, then the Count(1) and Count(*) will give different values.

    set nocount on
        declare @table1 table (empid int)
        insert @table1 values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(NULL),(11),(12),(NULL),(13),(14);
    
        select * from @table1
        select COUNT(1) as "COUNT(1)" from @table1
        select COUNT(empid) "Count(empid)" from @table1
    

    Query Results

    As you can see in the image, The first result shows the table has 16 rows. out of which two rows are NULL. So when we use Count(*) the query engine counts the number of rows, So we got count result as 16. But in case of Count(empid) it counted the non-NULL-values in the column empid. So we got the result as 14.

    so whenever we are using COUNT(Column) make sure we take care of NULL values as shown below.

    select COUNT(isnull(empid,1)) from @table1
    

    will count both NULL and Non-NULL values.

    Note: Same thing applies even when the table is made up of more than one column. Count(1) will give total number of rows irrespective of NULL/Non-NULL values. Only when the column values are counted using Count(Column) we need to take care of NULL values.

    0 讨论(0)
  • I created the table in postgres 10 and both of the following worked:

    select count(*) from us

    and

    select count(a is null) from us

    0 讨论(0)
提交回复
热议问题