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

zzwlnbp8  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(310)

我有一个表格,格式如下:

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

我想编写一个查询,统计每个用户提交、审阅和返回的记录,其中“submitted”是提交日期不为null、审阅和返回为null的任何记录“已审核”是指提交和审核日期不为空且返回日期为空的任何记录returned是提交、审核和返回日期不为空的任何记录。
预期输出如下:

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

我试着做了三个按用户名分组的独立计数查询,但是没有找到零。我对sql非常陌生,因此任何帮助都将不胜感激。

t0ybt7op

t0ybt7op1#

只是使用 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 .

oug3syen

oug3syen2#

试试这个:

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];

相关问题