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稍后在查询中使用的位置,但如果您需要底部的 Total 行,您可以使用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 回答