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,
ISNULL(COUNT(clnt_ntnlty), 0) AS Counts
FROM dbo.clientInfo
group by clnt_ntnlty

我得到的结果是 在此处输入图像描述

如果没有 value ,我希望所有(五行)记录为零。我已经尝试过所有人,它不起作用。

4

5 回答 5

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
于 2013-09-20T11:58:47.793 回答
0

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

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

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

于 2013-09-20T12:07:01.757 回答
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
于 2013-09-20T12:03:53.270 回答
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
于 2013-09-20T12:03:54.370 回答
0

如果我正确理解您的问题,那么您想在哪里计算“零”,那么这就是一个简单的答案。

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

至 -COUNT(ISNULL(clnt_ntnlty, 0)) AS Counts

如果这不是您要寻找的东西,那么将更具欺骗性。

于 2013-09-20T12:05:26.973 回答