按类别的记录计数,包括零

时间:2020-07-21 03:57:59

标签: sql sql-server tsql count

我有一个以下格式的表:

----------------------------------------------------
| Id | user_name | submitted  |  reviewed  |  returned  |
---------------------------------------------------------
| 1  | tom       | 01-01-2020 | 02-01-2020 |            |
| 2  | mary      | 01-15-2020 |            |            |
| 3  | joe       | 01-25-2020 | 02-07-2020 | 03-04-2020 |
| 4  | tom       | 01-07-2020 |            |            |
| 5  | tom       | 01-04-2020 |            |            |
| 6  | mary      | 01-16-2020 |            |            |
| 7  | joe       | 02-08-2020 | 02-08-2020 | 03-07-2020 |
| 8  | mary      | 01-05-2020 | 01-20-2020 | 03-19-2020 |
| 9  | joe       | 01-21-2020 | 02-09-2020 |            |
---------------------------------------------------------

我想编写一个查询,该查询计算每个用户的已提交,已审阅和返回记录,其中“已提交”是指提交日期不为null且审阅并返回为null的任何记录。 “审阅”是提交和审阅日期不为空且返回日期为空的任何记录。 “返回的所有记录的提交,查看和返回日期都不为空。

所需的输出如下:

-----------------------------------------------------
| user_name | # Submitted | # Reviewed | # Returned |
-----------------------------------------------------
| joe       |      0      |      1     |      2     |
| mary      |      2      |      0     |      1     |
| tom       |      2      |      1     |      0     |
-----------------------------------------------------

我尝试执行三个单独的按用户名分组的计数查询,但是这些查询遗漏了零。我是sql的新手,所以将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试一下:

DECLARE @DataSource TABLE
(
    [id] INT
   ,[user_name] NVARCHAR(128)
   ,[submitted] DATE 
   ,[reviewed] DATE 
   ,[returned] DATE
);

INSERT INTO @DataSource ([id], [user_name], [submitted], [reviewed], [returned])
VALUES (1, 'tom', '01-01-2020', '02-01-2020', NULL)
      ,(2, 'mary', '01-15-2020', NULL, NULL)
      ,(3, 'joe', '01-25-2020', '02-07-2020', '03-04-2020')
      ,(4, 'tom', '01-07-2020', NULL, NULL)
      ,(5, 'tom', '01-04-2020', NULL, NULL)
      ,(6, 'mary', '01-16-2020', NULL, NULL)
      ,(7, 'joe', '02-08-2020', '02-08-2020', '03-07-2020')
      ,(8, 'mary', '01-05-2020', '01-20-2020', '03-19-2020')
      ,(9, 'joe', '01-21-2020', '02-09-2020', NULL);

SELECT [user_name]
      ,SUM(IIF([returned] IS NULL AND [reviewed] IS NULL AND [submitted] IS NOT NULL, 1, 0)) AS [ # Submitted]
      ,SUM(IIF([returned] IS NULL AND [reviewed] IS NOT NULL AND [submitted] IS NOT NULL, 1, 0)) AS [# Reviewed]
      ,SUM(IIF([returned] IS NOT NULL AND [reviewed] IS NOT NULL AND [submitted] IS NOT NULL, 1, 0)) AS [# Returned]
FROM @DataSource
GROUP BY [user_name];

答案 1 :(得分:0)

只需使用count()。根据样本数据,您可以分别查看每列:

select user_name,
       count(submitted) as num_submitted,
       count(reviewed) as num_reviewed,
       count(returned) as num_returned
from t
group by user_name;

例如,没有示例,其中returned是非NULL,而其他任何一列是NULL

如果实际上可行,则可以使用条件聚合:

select user_name,
       count(submitted) as num_submitted,
       sum(case when submitted is not null and reviewed is not null then 1 else 0 end) as num_reviewed,
       sum(case when submitted is not null and reviewed is not null and returned is not null then 1 else 0 end) as num_returned
from t
group by user_name;

您还可以使用count()并通过算术玩游戏:

select user_name,
       count(submitted) as num_submitted,
       count(day(submitted) + day(reviewed)) as num_reviewed,
       count(day(submitted) + day(reviewed) + day(returned)) as num_returned
from t
group by user_name;

之所以可行,是因为如果值是day()NULL返回NULL。如果任何值为+,则NULL返回NULL