Counting null and non-null values in a single query

后端 未结 26 1078
星月不相逢
星月不相逢 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: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;
    

提交回复
热议问题