クライアントの 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
クエリの間違いを見つけるのを手伝ってください! ありがとうございました