如何在GROUP BY语句中返回零计数

时间:2018-07-11 12:09:42

标签: sql-server tsql group-by count null

我希望我的COUNT和GROUP BY语句返回零值

以下是我正在尝试做的简化版本:

SELECT DISTINCT 
,r.Team
,r.Peron_ID
,'Person Status' AS 'VariableType'
, CASE
        WHEN p.STATUS= 'Y' THEN 'Good'
        WHEN p.STATUS = 'N' THEN 'Bad'
        WHEN p.STATUS IS NULL OR p.STATUS NOT IN ('Y','N') THEN 'Invalid'
        END AS VariableA
  ,NULL AS VariableB
into #TC
from    Test r
INNER JOIN Person p ON r.RECORD_NUMBER = p.RECORD_NUMBER

Select 
,t.Team
,t.Person_ID
,VariableType
,variableA
,ISNULL(count(t.Person_ID),0) as Count
from #TC t
Group by t.team, t.Person_ID, variabletype, VariableA

举一个简单的例子,在下表中,Person1的每个VariableA都有一个数字,但是Person2没有任何一个条目:

+--------+-------------+---------------+-----------+-------+
| t.Team | t.Person_ID | VariableType  | VariableA | Count |
|--------+-------------+---------------+-----------+-------+
| Team1  | Person1     | Person Status | Good      |     2 |
| Team1  | Person1     | Person Status | Bad       |     3 |
| Team1  | Person1     | Person Status | Invalid   |     2 |
| Team1  | Person2     | Person Status | NULL      | NULL  |
+--------+-------------+---------------+-----------+-------+

我应该如何获取Person2的好,坏和无效计数显示为0?

请把上面的代码当作一种伪代码,我对我的实际代码进行了释义和简化,该代码是公司特定的,并且依赖于很多强制类型转换和大小写,因此语法可能并不准确。

感谢罗斯的回应。 Person2根本没有记录其P.STATUS。无论如何,我想这就是为什么他只显示为NULL。

我的预期(或只是希望的)结果是这样的:

+--------+-------------+---------------+-----------+-------+
| t.Team | t.Person_ID | VariableType  | VariableA | Count |
+--------+-------------+---------------+-----------+-------+
| Team1  | Person1     | Person Status | Good      |     2 |
| Team1  | Person1     | Person Status | Bad       |     3 |
| Team1  | Person1     | Person Status | Invalid   |     2 |
| Team1  | Person2     | Person Status | Good      |     0 |
| Team1  | Person2     | Person Status | Bad       |     0 |
| Team1  | Person2     | Person Status | Invalid   |     0 |
+--------+-------------+---------------+-----------+-------+

2 个答案:

答案 0 :(得分:0)

https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2005/ms175997(v=sql.90)

  

COUNT始终返回int数据类型值。 COUNT_BIG总是返回   bigint数据类型值。

计数始终不为空值

多重导致您的身份

DECLARE @a TABLE (a int, status varchar(255))
INSERT INTO @a VALUES (1, 'Good')

SELECT 
    a.a,
    x.Status, 
    SUM((CASE WHEN a.Status =x.Status THEN 1 ELSE 0 END)) Cnt
FROM @a a 
CROSS JOIN
(
    SELECT 'Good' UNION ALL
    SELECT 'Bad' UNION ALL
    SELECT 'Invalid'
) as x(Status)
GROUP  BY 
    a.a,
    x.Status

答案 1 :(得分:0)

如果要为人员/团队/变量类型的每种组合显示变量A的3个唯一值,则需要使用它们创建一个内联视图,然后在CROSS JOIN中使用它:

    CREATE TABLE #Test(
    [RECORD_NUMBER] [int] NOT NULL,
    [Team] [nvarchar](50) NULL,
    [Person_ID] [int] NULL
);

CREATE TABLE #Person(
    [RECORD_NUMBER] [int] NOT NULL,
    [STATUS] [nvarchar](1) NULL
);

SELECT r.Team
,r.Person_ID
,'Person Status' AS 'VariableType'
, CASE
        WHEN p.STATUS= 'Y' THEN 'Good'
        WHEN p.STATUS = 'N' THEN 'Bad'
        WHEN p.STATUS IS NULL and p.RECORD_NUMBER IS NULL THEN NULL
        WHEN p.STATUS IS NULL OR p.STATUS NOT IN ('Y','N') THEN 'Invalid'
        END AS VariableA
  ,NULL AS VariableB
into #TC
from Test r 
LEFT JOIN Person p ON r.RECORD_NUMBER = p.RECORD_NUMBER;

SELECT 
    t.Team,
    t.Person_ID,
    t.VariableType,
    VarValues.VariableA, 
    SUM((CASE WHEN t.VariableA = VarValues.VariableA THEN 1 ELSE 0 END)) Cnt
FROM #TC t
CROSS JOIN
(
    SELECT 'Good' UNION ALL
    SELECT 'Bad' UNION ALL
    SELECT 'Invalid'
) as VarValues(VariableA)
GROUP  BY 
    t.Team,
    t.Person_ID,
    t.VariableType,
    VarValues.VariableA
ORDER BY t.Person_ID, VarValues.VariableA;

DROP TABLE #TC;
DROP TABLE #Test;
DROP TABLE #Person;