1

クライアントの CategoryCode に基づいてカウントを計算する手順があり、民族性は機能しているように見えますが、カテゴリ コードごとのカウントの一部が欠落しています これは手順です

alter PROCEDURE SelectTotalActiveClients
AS
declare @GrandTotal int 
set @GrandTotal = (select COUNT (clientID)from Clients where StatusID in (1,2))
BEGIN
    SET NOCOUNT ON;
    with native_cte (cat, cnt)
    AS
    (
        select c.Category, count(e.EthnCode) 'Native'
        from Category c, Ethnicity e, Clients cl
        where cl.CategCode = c.CategCode
        and cl.EthnCode = e.EthnCode
        and e.EthnCode = 'N'
        and cl.StatusID in (1,2)
        group by c.Category
    ),
    asian_cte (cat, cnt)
    AS
    (
        select c.Category, count(e.EthnCode) 'Asian'
        from Category c, Ethnicity e, Clients cl
        where cl.CategCode = c.CategCode
        and cl.EthnCode = e.EthnCode
        and e.EthnCode = 'A'
        and cl.StatusID in (1,2)
        group by c.Category
    ),
    black_cte (cat, cnt)
    AS
    (
        select c.Category, count(e.EthnCode) 'Black'
        from Category c, Ethnicity e, Clients cl
        where cl.CategCode = c.CategCode
        and cl.EthnCode = e.EthnCode
        and e.EthnCode = 'B'
        and cl.StatusID in (1,2)
        group by c.Category
    ),
    pacific_cte (cat, cnt)
    AS
    (
        select c.Category, count(e.EthnCode) 'Pacific'
        from Category c, Ethnicity e, Clients cl
        where cl.CategCode = c.CategCode
        and cl.EthnCode = e.EthnCode
        and e.EthnCode = 'P'
        and cl.StatusID in (1,2)
        group by c.Category
    ),
    white_cte (cat, cnt)
    AS
    (
        select c.Category, count(e.EthnCode) 'White'
        from Category c, Ethnicity e, Clients cl
        where cl.CategCode = c.CategCode
        and cl.EthnCode = e.EthnCode
        and e.EthnCode = 'W'
        and cl.StatusID in (1,2)
        group by c.Category
    ),
    multy_cte (cat, cnt)
    AS
    (
        select c.Category, count(e.EthnCode) 'Multy'
        from Category c, Ethnicity e, Clients cl
        where cl.CategCode = c.CategCode
        and cl.EthnCode = e.EthnCode
        and e.EthnCode not IN ('N', 'A', 'B', 'P', 'W', '0')
        and cl.StatusID in (1,2)
        group by c.Category
    ),
    unknown_cte (cat, cnt)
    AS
    (
        select c.Category, count(e.EthnCode) 'Unknown'
        from Category c, Ethnicity e, Clients cl
        where cl.CategCode = c.CategCode
        and cl.EthnCode = e.EthnCode
        and e.EthnCode = '0'
        and cl.StatusID in (1,2)
        group by c.Category
    ),
        total_cte (cat, cnt)
    AS
    (
        select c.Category, count(e.EthnCode) 'Total'
        from Category c, Ethnicity e, Clients cl
        where cl.CategCode = c.CategCode
        and cl.EthnCode = e.EthnCode
        and cl.StatusID in (1,2)
        group by c.Category
    )
    SELECT  Category, native_cte.cnt 'Native', asian_cte.cnt 'Asian', 
            black_cte.cnt 'Black', pacific_cte.cnt 'Pacific', white_cte.cnt 'White', multy_cte.cnt 'Multy',
            unknown_cte.cnt 'Unknown', total_cte.cnt as 'Total'
    FROM    Category 
    left outer  JOIN native_cte on Category.Category = native_cte.cat
    left outer  JOIN asian_cte on native_cte.cat = asian_cte.cat
        left outer  join black_cte on asian_cte.cat = black_cte.cat
                left outer join pacific_cte on black_cte.cat = pacific_cte.cat
                    left outer join white_cte on pacific_cte.cat = white_cte.cat
                        left outer join multy_cte on white_cte.cat = multy_cte.cat
                             left outer join unknown_cte on multy_cte.cat = unknown_cte.cat
                                left outer join total_cte on unknown_cte.cat  = total_cte.cat
END
GO

結果が得られます:

Child     NULL  NULL    NULL    NULL    NULL    NULL    NULL    NULL
Infant    NULL  NULL    NULL    NULL    NULL    NULL    NULL    NULL
Newborn   NULL  NULL    NULL    NULL    NULL    NULL    NULL    NULL
Pregnant      2 NULL    NULL    NULL    NULL    NULL    NULL    NULL
Postpartum    1 NULL    NULL    NULL    NULL    NULL    NULL    NULL
Senior      220 188       36     11      485     12      44  996

ただし、このcte selectを自分で実行すると、たとえば異なる応答が得られます

select c.Category, count(e.EthnCode) 'White'
        from Category c, Ethnicity e, Clients cl
        where cl.CategCode = c.CategCode
        and cl.EthnCode = e.EthnCode
        and e.EthnCode = 'W'
        and cl.StatusID in (1,2)
        group by c.Category

Postpartum  4
Pregnant    2
Senior      485

クエリの間違いを見つけるのを手伝ってください! ありがとうございました

4

2 に答える 2

3

クエリ全体を次のように簡略化できると思います。

SELECT  c.Category,
        [Native] = COUNT(CASE WHEN cl.EthnCode = 'N' THEN cl.EthnCode END),
        [Asian] = COUNT(CASE WHEN cl.EthnCode = 'A' THEN cl.EthnCode END),
        [Black] = COUNT(CASE WHEN cl.EthnCode = 'B' THEN cl.EthnCode END),
        [Pacific] = COUNT(CASE WHEN cl.EthnCode = 'P' THEN cl.EthnCode END),
        [White] = COUNT(CASE WHEN cl.EthnCode = 'W' THEN cl.EthnCode END),
        [Multy] = COUNT(CASE WHEN cl.EthnCode NOT IN ('N', 'A', 'B', 'P', 'W', '0') THEN cl.EthnCode END),
        [Unknown] = COUNT(CASE WHEN cl.EthnCode = '0' THEN cl.EthnCode END),
        [Total] = COUNT(cl.EthnCode)
FROM    Category c, 
        LEFT JOIN Clients cl
            ON cl.CategCode = c.CategCode
            AND cl.StatusID IN (1, 2)
GROUP BY Category

これにより、同じ結果が得られ、パフォーマンスが大幅に向上し、(私の意見では) はるかに読みやすくなります。

クエリの後半で使用する場所はわかりませんが@GrandTotal、下部に合計行が必要な場合は、WITH ROLLUPを使用できます。

SELECT  [Category] = ISNULL(c.Category, 'Total'),
        [Native] = COUNT(CASE WHEN cl.EthnCode = 'N' THEN cl.EthnCode END),
        [Asian] = COUNT(CASE WHEN cl.EthnCode = 'A' THEN cl.EthnCode END),
        [Black] = COUNT(CASE WHEN cl.EthnCode = 'B' THEN cl.EthnCode END),
        [Pacific] = COUNT(CASE WHEN cl.EthnCode = 'P' THEN cl.EthnCode END),
        [White] = COUNT(CASE WHEN cl.EthnCode = 'W' THEN cl.EthnCode END),
        [Multy] = COUNT(CASE WHEN cl.EthnCode NOT IN ('N', 'A', 'B', 'P', 'W', '0') THEN cl.EthnCode END),
        [Unknown] = COUNT(CASE WHEN cl.EthnCode = '0' THEN cl.EthnCode END),
        [Total] = COUNT(cl.EthnCode)
FROM    Category c, 
        LEFT JOIN Clients cl
            ON cl.CategCode = c.CategCode
            AND cl.StatusID IN (1, 2)
GROUP BY Category
WITH ROLLUP;
于 2013-03-05T09:49:34.890 に答える
1

これを試してください。左側の結合はカテゴリで作成する必要があります:

FROM    Category 
left outer  JOIN native_cte on Category.Category = native_cte.cat
left outer  JOIN asian_cte on Category.Category = asian_cte.cat
left outer  join black_cte on Category.Category = black_cte.cat
left outer join pacific_cte on Category.Category = pacific_cte.cat
left outer join white_cte on Category.Category = white_cte.cat
left outer join multy_cte on Category.Category = multy_cte.cat
left outer join unknown_cte on Category.Category = unknown_cte.cat
left outer join total_cte on Category.Category  = total_cte.cat
于 2013-03-05T09:40:45.973 に答える