数据仓库 - 树形结构的维表开发实践

一、概述

根据星型模型的概念,不存在渐变维度,数据存在冗,典型例子地域维度表,如国家,省,市这种树形数据结构。

OLTP数据结构:

id pid name
1   中国
2 1 广东省
3 2 深圳

期望的星型模型数据结构:

id country province city level
1 中国 UNKNOWN UNKNOWN 1
2 中国 广东省 UNKNOWN 2
3 中国 广东省 深圳 3

二、GreenPlum处理方案

gp底层是PostgreSQL, 支持递归,实现如下:

WITH RECURSIVE cte AS (
	SELECT
		"id",
		"name",
		1 AS "level"
	FROM
		t_location
	UNION ALL
		SELECT
			"c"."id",
			(
				"p"."name" || '/' || "c"."name"
			) :: VARCHAR (255) AS "name",
			"p"."level" + 1 AS "level"
		FROM
			cte AS "p"
		JOIN t_location AS "c" ON "c"."pid" = "p"."id"
) 

SELECT
	"id",
	split_part("name", '/', 1) AS country,
	split_part("name", '/', 2) AS province,
	split_part("name", '/', 3) AS city,
	"level"
FROM
	(
		SELECT
			"c"."id",
			"c"."name",
			"c"."level"
		FROM
			cte "c"
		INNER JOIN (
			SELECT
				"id",
				MAX ("level") AS "lv"
			FROM
				cte
			GROUP BY
				"id"
		) AS "d" ON "c"."id" = "d"."id"
		AND "c"."level" = "d"."lv"
	) AS "t"

源数据:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值