我有一个表层次结构:
GrandParentFoo
,零或更多ParentFoo
,零或更多ChildFoo
ParentFoo
和ChildFoo
都有一个Status
列,总共有4种可能的状态:
我正在尝试编写一个查询,它给出了以下几行中任何特定GrandParentFoo
的汇总:
我开始走的路:
select
gp.GrandParentFooId,
count(distinct pf.ParentFooId) as TotalParentFoos,
sum(case pf.Status
when 1 then 1
else 0 end) as TotalParentFoosPending
...当我意识到这会给我一个夸大的数量,其中ChildFoo
记录中存在多个ParentFoo
记录。
我是否必须将其作为一系列CTE写出来,还是有更简洁的方法来做到这一点?看起来某种类型的枢轴或窗口函数在这里可以工作,但我无法概念化它。
一个相对简单的方法使用count(distinct)
的条件聚合:
select gp.GrandParentFooId,
count(distinct pf.ParentFooId) as TotalParentFoos,
count(distinct case when fp.status = 1 then pf.ParentFooId end) as parent_pending,
count(distinct case when fp.status = 2 then pf.ParentFooId end) as parent_active,
count(distinct case when fp.status = 3 then pf.ParentFooId end) as parent_paused,
count(distinct case when fp.status = 4 then pf.ParentFooId end) as parent_completed,
count(distinct c.ChildId) as num_children,
count(distinct case when fp.status = 1 then c.ChildId end) as child_pending,
count(distinct case when fp.status = 2 then c.ChildId end) as child_active,
count(distinct case when fp.status = 3 then c.ChildId end) as child_paused,
count(distinct case when fp.status = 4 then c.ChildId end) as child_completed
from grandparentfoo gp left join
parentfoo p
on gp.GrandParentFooId = p.GrandParentFooId left join
childfoo c
on p.ParentFooId = c.ParentFooId;
笔记:
COUNT(DISTINCT)
。 COUNT(c.ChildId)
可能就足够了。COUNT(DISTINCT)
s。