在单个查询中计算null和非null值

时间:2009-08-13 12:59:13

标签: sql

我有一张桌子

create table us
(
 a number
);

现在我有以下数据:

a
1
2
3
4
null
null
null
8
9

现在我需要一个查询来计算列a中的空非空值

26 个答案:

答案 0 :(得分:199)

这适用于Oracle和SQL Server(您可以将其用于另一个RDBMS):

select sum(case when a is null then 1 else 0 end) count_nulls
     , count(a) count_not_nulls 
  from us;

或者:

select count(*) - count(a), count(a) from us;

答案 1 :(得分:52)

如果我理解正确,你想要计算列中的所有NULL和所有NOT NULL ......

如果这是正确的:

SELECT count(*) FROM us WHERE a IS NULL 
UNION ALL
SELECT count(*) FROM us WHERE a IS NOT NULL

在阅读评论后编辑完整查询:]


SELECT COUNT(*), 'null_tally' AS narrative 
  FROM us 
 WHERE a IS NULL 
UNION
SELECT COUNT(*), 'not_null_tally' AS narrative 
  FROM us 
 WHERE a IS NOT NULL;

答案 2 :(得分:38)

这是一个适用于Oracle的快速且脏的版本:

select sum(case a when null then 1 else 0) "Null values",
       sum(case a when null then 0 else 1) "Non-null values"
from us

答案 3 :(得分:19)

我理解你的查询,你只需运行这个脚本并获得Total Null,Total NotNull行,

select count(*) - count(a) as 'Null', count(a) as 'Not Null' from us;

答案 4 :(得分:14)

通常我会使用这个技巧

select sum(case when a is null then 0 else 1 end) as count_notnull,
       sum(case when a is null then 1 else 0 end) as count_null
from tab
group by a

答案 5 :(得分:6)

表示非空值

select count(a)
from us

表示空值

select count(*)
from us

minus 

select count(a)
from us

因此

SELECT COUNT(A) NOT_NULLS
FROM US

UNION

SELECT COUNT(*) - COUNT(A) NULLS
FROM US

应该做的工作

更好的是,列标题正确无误。

SELECT COUNT(A) NOT_NULL, COUNT(*) - COUNT(A) NULLS
FROM US

在我的系统上进行的某些测试中,需要进行全表扫描。

答案 6 :(得分:5)

这有点棘手。假设表只有一列,则Count(1)和Count(*)将给出不同的值。

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

正如您在图像中看到的那样,第一个结果显示该表有16行。其中两行为NULL。因此,当我们使用 Count(*)时,查询引擎计算行数,因此我们将计数结果计为16.但是在 Count(empid)的情况下,它计算了列 empid 中的非NULL值。所以我们得到了14的结果。

所以每当我们使用COUNT(Column)时,请确保我们处理NULL值,如下所示。

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

将计算NULL和非NULL值。

注意:即使表格由多个列组成,也同样适用。 Count(1)将给出总行数,而不管NULL /非NULL值。只有当使用Count(Column)计算列值时,我们才需要处理NULL值。

答案 7 :(得分:4)

仅提供另一种选择,Postgres 9.4 + allows applying a FILTER to aggregates

SELECT
  COUNT(*) FILTER (WHERE a IS NULL) count_nulls,
  COUNT(*) FILTER (WHERE a IS NOT NULL) count_not_nulls
FROM us;

SQLFiddle:http://sqlfiddle.com/#!17/80a24/5

答案 8 :(得分:3)

我遇到了类似的问题:计算所有不同的值,将空值计算为1。在这种情况下,简单计数不起作用,因为它不考虑空值。

这是一个适用于SQL的代码片段,不涉及选择新值。 基本上,一旦执行了distinct,也使用row_number()函数返回新列(n)中的行号,然后对该列执行计数:

SELECT COUNT(n)
FROM (
    SELECT *, row_number() OVER (ORDER BY [MyColumn] ASC) n
    FROM (
        SELECT DISTINCT [MyColumn]
                    FROM [MyTable]
        ) items  
) distinctItems

答案 9 :(得分:2)

尝试

SELECT 
   SUM(ISNULL(a)) AS all_null,
   SUM(!ISNULL(a)) AS all_not_null
FROM us;

简单!

答案 10 :(得分:2)

尝试一下。

SELECT CASE 
         WHEN a IS NULL THEN 'Null' 
         ELSE 'Not Null' 
       END a, 
       Count(1) 
FROM   us 
GROUP  BY CASE 
            WHEN a IS NULL THEN 'Null' 
            ELSE 'Not Null' 
          END 

答案 11 :(得分:2)

以下是两个解决方案:

Select count(columnname) as countofNotNulls, count(isnull(columnname,1))-count(columnname) AS Countofnulls from table name

OR

Select count(columnname) as countofNotNulls, count(*)-count(columnname) AS Countofnulls from table name

答案 12 :(得分:2)

使用ISNULL嵌入式功能。


答案 13 :(得分:2)

如果您正在使用MS Sql Server ...

SELECT COUNT(0) AS 'Null_ColumnA_Records',
(
    SELECT COUNT(0)
    FROM your_table
    WHERE ColumnA IS NOT NULL
) AS 'NOT_Null_ColumnA_Records'
FROM your_table
WHERE ColumnA IS NULL;

我不建议你这样做......但是你在这里(与结果在同一张表中)

答案 14 :(得分:1)

select count(isnull(NullableColumn,-1))

答案 15 :(得分:1)

这适用于T-SQL。如果您只计算某些内容并且想要包含空值,请使用COALESCE而不是大小写。

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')

答案 16 :(得分:1)

建立了Alberto,我添加了汇总。

 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;

答案 17 :(得分:1)

SELECT SUM(NULLs) AS 'NULLS', SUM(NOTNULLs) AS 'NOTNULLs' FROM 
    (select count(*) AS 'NULLs', 0 as 'NOTNULLs' FROM us WHERE a is null
    UNION select 0 as 'NULLs', count(*) AS 'NOTNULLs' FROM us WHERE a is not null) AS x

这很难,但它会返回一条带有2个cols的记录,表示空值与非空值的计数。

答案 18 :(得分:1)

SELECT
    ALL_VALUES
    ,COUNT(ALL_VALUES)
FROM(
        SELECT 
        NVL2(A,'NOT NULL','NULL') AS ALL_VALUES 
        ,NVL(A,0)
        FROM US
)
GROUP BY ALL_VALUES

答案 19 :(得分:1)

所有答案都是错误的或过时的。

执行此查询的简单正确的方法是使用COUNT_IF函数。

SELECT
  COUNT_IF(a IS NULL) AS nulls,
  COUNT_IF(a IS NOT NULL) AS not_nulls
FROM
  us

答案 20 :(得分:1)

如果是mysql,你可以试试这样的东西。

select 
   (select count(*) from TABLENAME WHERE a = 'null') as total_null, 
   (select count(*) from TABLENAME WHERE a != 'null') as total_not_null
FROM TABLENAME

答案 21 :(得分:1)

我在postgres 10中创建了表,并且以下两项均有效:

select count(*) from us

select count(a is null) from us

答案 22 :(得分:0)

用于计算非空值

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

用于计算空值

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

答案 23 :(得分:0)

以防万一你想要一个记录:

select 
  (select count(*) from tbl where colName is null) Nulls,
  (select count(*) from tbl where colName is not null) NonNulls 

- )

答案 24 :(得分:0)

就我而言,我希望在多个列之间使用“ 空分布”:

case 'MODIFIED_GROUPS':
            return { ...state, arrayGroups: action.payload }

根据'...',它可以轻松扩展到更多列,并根据需要扩展

答案 25 :(得分:-1)

a为null的元素数:

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

a不为null的元素数:

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