如何在sql中获取没有值的零

时间:2013-09-20 11:53:43

标签: sql sql-server-2005

我的查询是

SELECT CASE
WHEN clnt_ntnlty =0 THEN 'Aboriginal'
WHEN clnt_ntnlty =1 THEN 'Torres Strait Islander'
WHEN clnt_ntnlty =2 THEN 'Both Aboring & Torres Strait' 
WHEN clnt_ntnlty =3 THEN 'Neither Aboring OR Torres Strait'
ELSE 'Not Provided' END AS Identy,
ISNULL(COUNT(clnt_ntnlty), 0) AS Counts
FROM dbo.clientInfo
group by clnt_ntnlty

我得到了结果 enter image description here

如果没有值,我希望所有(五行)记录为零。我和所有人一起尝试过,它没有用。

5 个答案:

答案 0 :(得分:0)

您可以拥有一个子查询,其中包含您想要的四个值,并在其上执行LEFT JOIN

SELECT  a.Identy,
        COALESCE(b.Counts, 0) Counts
FROM    (
            SELECT 'Aboriginal' Identy
            UNION ALL
            SELECT 'Torres Strait Islander' Identy
            UNION ALL
            SELECT 'Both Aboring & Torres Strait' Identy
            UNION ALL 
            SELECT 'Neither Aboring OR Torres Strait' Identy
        ) a LEFT JOIN
        (
            SELECT  CASE
                        WHEN clnt_ntnlty =0 THEN 'Aboriginal'
                        WHEN clnt_ntnlty =1 THEN 'Torres Strait Islander'
                        WHEN clnt_ntnlty =2 THEN 'Both Aboring & Torres Strait' 
                        WHEN clnt_ntnlty =3 THEN 'Neither Aboring OR Torres Strait'
                        ELSE 'Not Provided' END AS Identy,
                        ISNULL(COUNT(clnt_ntnlty), 0) AS Counts
            FROM dbo.clientInfo
            group by clnt_ntnlty
        ) b ON a.Identy = b.Identy

答案 1 :(得分:0)

SELECT CASE
WHEN clnt_ntnlty =0 THEN 'Aboriginal'
WHEN clnt_ntnlty =1 THEN 'Torres Strait Islander'
WHEN clnt_ntnlty =2 THEN 'Both Aboring & Torres Strait' 
WHEN clnt_ntnlty =3 THEN 'Neither Aboring OR Torres Strait'
ELSE 'Not Provided' END AS Identy,
SUM(CASE WHEN I.clnt_ntnlty IS NOT NULL THEN 1 ELSE 0 END) AS c_ntnlty
FROM 
(VALUES (0),(1),(2),(3),(9)) N (ntnlty) LEFT JOIN
dbo.clientInfo I ON N.ntnlty = ISNULL(I.clnt_ntnlty,9)

GROUP BY N.ntnlty

答案 2 :(得分:0)

请尝试:

SELECT 
    DISTINCT Identy, 
    COUNT(clnt_ntnlty) OVER (PARTITION BY Identy) Counts
FROM (
        SELECT 0 Val, 'Aboriginal' Identy UNION
        SELECT 1 Val, 'Torres Strait Islander' Identy UNION
        SELECT 2 Val, 'Both Aboring & Torres Strait' Identy UNION
        SELECT 3 Val, 'Neither Aboring OR Torres Strait' Identy 
    ) x left join dbo.clientInfo t on t.clnt_ntnlty=x.Val

答案 3 :(得分:0)

如果我理解你正确地问你想要计算"零"那么这就是一个简单的答案。

更改 - ISNULL(COUNT(clnt_ntnlty), 0) AS Counts

收件人 - COUNT(ISNULL(clnt_ntnlty, 0)) AS Counts

如果这不是你想要的东西那么会更具说明性。

答案 4 :(得分:0)

好的,我不会为以下脚本感到自豪,因为它是一个肮脏的解决方案;联合所有可能的值并减去1以删除该添加

SELECT CASE
WHEN cte.clnt_ntnlty =0 THEN 'Aboriginal'
WHEN cte.clnt_ntnlty =1 THEN 'Torres Strait Islander'
WHEN cte.clnt_ntnlty =2 THEN 'Both Aboring & Torres Strait' 
WHEN cte.clnt_ntnlty =3 THEN 'Neither Aboring OR Torres Strait'
ELSE 'Not Provided' END AS Identy,
ISNULL(COUNT(cte.clnt_ntnlty) - 1, 0) AS Counts
FROM (select ID,clnt_ntnlty from dbo.clientInfo
union select 0,'0' as clnt_ntnlty
union select 1, '1' as clnt_ntnlty
union select 2, '2' as clnt_ntnlty
union select 3, '3' as clnt_ntnlty) as cte
group by clnt_ntnlty

更改联合选择以匹配您的架构