我有一个表层次结构:
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写出来,还是有更简洁的方法来做到这一点?看起来某种类型的枢轴或窗口函数可以在这里工作,但我无法将其概念化。
答案 0 :(得分:1)
一种相对简单的方法是使用带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)
。