爸爸在哪儿?(皇位继承图)

image.png

开创新朝

节点的边框颜色或线段颜色有变的视为开创了新王朝,图示绿色所示的都是新王朝。
值得注意的是长剪头的7个类似五代十国,要视为一个整体(取长兄作为朝代名)


image.png

数据的iamroot计算出了上图的绿色点(新朝开创者)

-- 跟父亲的type、class不同或没有父亲的为本朝的root
create table find_father_root as 
select a.*,case when (a.po_type<>b.po_type or a.tiny_class <> b.tiny_class or b.style_id is null) then a.style_id end as iamroot
from ods.sku_path1030 a
left join ods.sku_path1030 b on a.repeat_style_id=b.style_id --b是父表
where a.style_path like '%117201600%'
order by a.style_path

填充空白(找祖宗,本朝的)

create table find_father_myroot as 
select a.style_id,a.repeat_style_id,a.po_type,a.style_path,coalesce(a.iamroot,max(b.iamroot)) my_root
from find_father_root a 
left join find_father_root b on instr(a.style_path,b.iamroot)>0
group by a.style_id,a.repeat_style_id,a.po_type,a.style_path,a.iamroot
image.png

朝代合并

potype为fast且父辈的root相同的予以合并


image.png
select a.style_id,a.style_path,b.my_root froot,a.my_root 
, min(a.my_root) over (partition by b.my_root) new_root
from find_father_myroot a
left join find_father_myroot b on a.repeat_style_id=b.style_id
order by a.style_path

最终结果


image.png

image.png

小调整:非fast的不要合并

select a.style_id,a.style_path,b.my_root froot,a.my_root 
,case when a.po_type='Fast-track Repeat' then min(a.my_root) over (partition by a.po_type,c.my_root) else a.my_root end as new_root
from find_father_myroot a
left join find_father_myroot b on a.my_root=b.style_id --找上个朝代
left join find_father_myroot c on b.repeat_style_id=c.style_id --找上个朝代的开国
order by a.style_path
image.png

dag图

-- 画graph----------------------------------------------
create table ods.sku_path_dot as --drop table ods.sku_path_dot
select  repeat_style_id ,style_id ,po_type,tiny_class,style_path,root_style_id,
    dense_rank() over(partition by root_style_id order by po_type ) po_type_rk,
    dense_rank() over(partition by root_style_id order by tiny_class ) tiny_class_rk
from ods.sku_path1030
-- 点
select distinct concat('"',style_id,'"',
    case when tiny_class_rk=1 then '[color=red]' 
    when tiny_class_rk=2 then '[color=yellow]'
    when tiny_class_rk=3 then '[color=green]'
    when tiny_class_rk=4 then '[color=blue]'
end) as graphviz
from sku_path_dot where style_path like '%117201600%' --and repeat_style_id is not null
-- 边
union all
select concat('"',repeat_style_id,'" -> "' ,style_id,'"',case when po_type='Fast-track Repeat' then '[color=pink]' else '' end) as graphviz
from sku_path_dot where style_path like '%117201600%' and repeat_style_id is not null
union all 
select '}'

http://www.webgraphviz.com/
登录网址在线出图:
复制下面2行,再加上上面代码跑出来的结果(本想放sql一起,顺序会乱)。

digraph G {
rankdir=LR
#上面sql结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,579评论 16 22
  • 创业是很多人的梦想,多少人为了理想和不甘选择了创业来实现自我价值,我就是其中一个。 创业后,我由女人变成了超人,什...
    亦宝宝阅读 1,878评论 4 1
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,617评论 0 11
  • 可爱进取,孤独成精。努力飞翔,天堂翱翔。战争美好,孤独进取。胆大飞翔,成就辉煌。努力进取,遥望,和谐家园。可爱游走...
    赵原野阅读 2,789评论 1 1