我有程序应该根据客户 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
请帮我找出查询中的错误!谢谢