查询哪些计数缺少一些数字

时间:2013-03-05 09:35:41

标签: sql sql-server stored-procedures

我有程序应该根据客户的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 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

请帮我在查询中找到错误!谢谢

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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