创建层次结构时可以 "connect by" 进行 运行 计算
Can "connect by" do running calculations when creating a hierarchy
我正在尝试创建一个可以在我的数据中创建事件层次结构的查询。但是,由于每条生产记录都可能出现多次,因此我需要为每个层次结构创建一个权重,这样我就知道哪个层次结构是正确的,以后可以过滤指定的结果。
此权重将是每个节点的距离之和,定义为启动和发生之间的差的绝对值之和。
如果启动和发生之间存在完美的关系,我会用它作为 connect by 语句中的标准,但事实并非如此,我想知道是否有可能对每个的绝对值求和节点以便我可以获得“准确性”度量以用作进一步分析的过滤列?
数据
PROD
MGRID
init
occ
100
NULL
2001/1/1
2016/4/12
100
NULL
2001/1/1
2017/2/11
100
NULL
2001/1/1
2017/7/19
101
100
2017/2/11
2017/2/21
102
100
2017/2/11
2017/2/11
103
100
2016/5/12
2016/5/12
201
103
2016/5/12
2016/5/12
202
101
2017/2/20
2017/2/21
203
100
2017/7/20
2017/7/19
当前代码
SELECT
prod, mgrid, init, occ, level AS lvl, sys_connect_by_path(prod, '/') AS path
FROM mytab
WHERE level > 1
START WITH
mgrid IS NULL
CONNECT BY
PRIOR prod = mgrid
ORDER BY
prod;
使用递归子查询:
WITH data (prod, mgrid, init, occ, lvl, path, weight) AS (
SELECT prod,
mgrid,
init,
occ,
1,
'/' || prod,
ABS(occ-init)
FROM mytab
WHERE mgrid IS NULL
UNION ALL
SELECT m.prod,
m.mgrid,
m.init,
m.occ,
lvl + 1,
d.path || '/' || m.prod,
d.weight + ABS(m.occ - m.init)
FROM mytab m
INNER JOIN data d
ON (d.prod = m.mgrid)
)
SELECT *
FROM data
WHERE lvl > 1
ORDER BY prod;
其中,对于您的样本数据:
CREATE TABLE mytab (PROD, MGRID, init, occ) AS
SELECT 100, NULL, DATE '2001-01-01', DATE '2016-04-12' FROM DUAL UNION ALL
SELECT 100, NULL, DATE '2001-01-01', DATE '2017-02-11' FROM DUAL UNION ALL
SELECT 100, NULL, DATE '2001-01-01', DATE '2017-07-19' FROM DUAL UNION ALL
SELECT 101, 100, DATE '2017-02-11', DATE '2017-02-21' FROM DUAL UNION ALL
SELECT 102, 100, DATE '2017-02-11', DATE '2017-02-11' FROM DUAL UNION ALL
SELECT 103, 100, DATE '2016-05-12', DATE '2016-05-12' FROM DUAL UNION ALL
SELECT 201, 103, DATE '2016-05-12', DATE '2016-05-12' FROM DUAL UNION ALL
SELECT 202, 101, DATE '2017-02-20', DATE '2017-02-21' FROM DUAL UNION ALL
SELECT 203, 100, DATE '2017-07-20', DATE '2017-07-19' FROM DUAL;
输出:
PROD
MGRID
INIT
OCC
LVL
PATH
WEIGHT
101
100
11-FEB-17
21-FEB-17
2
/100/101
5590
101
100
11-FEB-17
21-FEB-17
2
/100/101
6053
101
100
11-FEB-17
21-FEB-17
2
/100/101
5895
102
100
11-FEB-17
11-FEB-17
2
/100/102
5580
102
100
11-FEB-17
11-FEB-17
2
/100/102
6043
102
100
11-FEB-17
11-FEB-17
2
/100/102
5885
103
100
12-MAY-16
12-MAY-16
2
/100/103
5580
103
100
12-MAY-16
12-MAY-16
2
/100/103
6043
103
100
12-MAY-16
12-MAY-16
2
/100/103
5885
201
103
12-MAY-16
12-MAY-16
3
/100/103/201
5580
201
103
12-MAY-16
12-MAY-16
3
/100/103/201
6043
201
103
12-MAY-16
12-MAY-16
3
/100/103/201
5885
202
101
20-FEB-17
21-FEB-17
3
/100/101/202
6054
202
101
20-FEB-17
21-FEB-17
3
/100/101/202
5896
202
101
20-FEB-17
21-FEB-17
3
/100/101/202
5591
203
100
20-JUL-17
19-JUL-17
2
/100/203
5581
203
100
20-JUL-17
19-JUL-17
2
/100/203
5886
203
100
20-JUL-17
19-JUL-17
2
/100/203
6044
db<>fiddle here
我正在尝试创建一个可以在我的数据中创建事件层次结构的查询。但是,由于每条生产记录都可能出现多次,因此我需要为每个层次结构创建一个权重,这样我就知道哪个层次结构是正确的,以后可以过滤指定的结果。
此权重将是每个节点的距离之和,定义为启动和发生之间的差的绝对值之和。
如果启动和发生之间存在完美的关系,我会用它作为 connect by 语句中的标准,但事实并非如此,我想知道是否有可能对每个的绝对值求和节点以便我可以获得“准确性”度量以用作进一步分析的过滤列?
数据
PROD | MGRID | init | occ |
---|---|---|---|
100 | NULL | 2001/1/1 | 2016/4/12 |
100 | NULL | 2001/1/1 | 2017/2/11 |
100 | NULL | 2001/1/1 | 2017/7/19 |
101 | 100 | 2017/2/11 | 2017/2/21 |
102 | 100 | 2017/2/11 | 2017/2/11 |
103 | 100 | 2016/5/12 | 2016/5/12 |
201 | 103 | 2016/5/12 | 2016/5/12 |
202 | 101 | 2017/2/20 | 2017/2/21 |
203 | 100 | 2017/7/20 | 2017/7/19 |
当前代码
SELECT
prod, mgrid, init, occ, level AS lvl, sys_connect_by_path(prod, '/') AS path
FROM mytab
WHERE level > 1
START WITH
mgrid IS NULL
CONNECT BY
PRIOR prod = mgrid
ORDER BY
prod;
使用递归子查询:
WITH data (prod, mgrid, init, occ, lvl, path, weight) AS (
SELECT prod,
mgrid,
init,
occ,
1,
'/' || prod,
ABS(occ-init)
FROM mytab
WHERE mgrid IS NULL
UNION ALL
SELECT m.prod,
m.mgrid,
m.init,
m.occ,
lvl + 1,
d.path || '/' || m.prod,
d.weight + ABS(m.occ - m.init)
FROM mytab m
INNER JOIN data d
ON (d.prod = m.mgrid)
)
SELECT *
FROM data
WHERE lvl > 1
ORDER BY prod;
其中,对于您的样本数据:
CREATE TABLE mytab (PROD, MGRID, init, occ) AS
SELECT 100, NULL, DATE '2001-01-01', DATE '2016-04-12' FROM DUAL UNION ALL
SELECT 100, NULL, DATE '2001-01-01', DATE '2017-02-11' FROM DUAL UNION ALL
SELECT 100, NULL, DATE '2001-01-01', DATE '2017-07-19' FROM DUAL UNION ALL
SELECT 101, 100, DATE '2017-02-11', DATE '2017-02-21' FROM DUAL UNION ALL
SELECT 102, 100, DATE '2017-02-11', DATE '2017-02-11' FROM DUAL UNION ALL
SELECT 103, 100, DATE '2016-05-12', DATE '2016-05-12' FROM DUAL UNION ALL
SELECT 201, 103, DATE '2016-05-12', DATE '2016-05-12' FROM DUAL UNION ALL
SELECT 202, 101, DATE '2017-02-20', DATE '2017-02-21' FROM DUAL UNION ALL
SELECT 203, 100, DATE '2017-07-20', DATE '2017-07-19' FROM DUAL;
输出:
PROD MGRID INIT OCC LVL PATH WEIGHT 101 100 11-FEB-17 21-FEB-17 2 /100/101 5590 101 100 11-FEB-17 21-FEB-17 2 /100/101 6053 101 100 11-FEB-17 21-FEB-17 2 /100/101 5895 102 100 11-FEB-17 11-FEB-17 2 /100/102 5580 102 100 11-FEB-17 11-FEB-17 2 /100/102 6043 102 100 11-FEB-17 11-FEB-17 2 /100/102 5885 103 100 12-MAY-16 12-MAY-16 2 /100/103 5580 103 100 12-MAY-16 12-MAY-16 2 /100/103 6043 103 100 12-MAY-16 12-MAY-16 2 /100/103 5885 201 103 12-MAY-16 12-MAY-16 3 /100/103/201 5580 201 103 12-MAY-16 12-MAY-16 3 /100/103/201 6043 201 103 12-MAY-16 12-MAY-16 3 /100/103/201 5885 202 101 20-FEB-17 21-FEB-17 3 /100/101/202 6054 202 101 20-FEB-17 21-FEB-17 3 /100/101/202 5896 202 101 20-FEB-17 21-FEB-17 3 /100/101/202 5591 203 100 20-JUL-17 19-JUL-17 2 /100/203 5581 203 100 20-JUL-17 19-JUL-17 2 /100/203 5886 203 100 20-JUL-17 19-JUL-17 2 /100/203 6044
db<>fiddle here