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

私は結果を得ています ここに画像の説明を入力

値がない場合は、すべて(5行)のレコードがゼロになります。私はすべてグループで試しましたが、うまくいきません。

4

5 に答える 5

0

必要な4つの値を持つサブクエリを作成し、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 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 に答える