Oracle SQL,在分层查询中获得重复结果

时间:2017-10-30 14:09:56

标签: sql oracle

我对两个表<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>405 Method Not Allowed</title> </head><body> <h1>Method Not Allowed</h1> <p>The requested method PUT is not allowed for the URL /index.html.</p> <hr> <address>Apache/2.4.18 (Ubuntu) Server at 192.168.1.50 Port 80</address> </body></html> workorder进行了以下查询。我正在努力获得一个工作订单,它的ID等于classstructure以及它的分类路径。 (该路径的父级是000000004140094)。

1688

我怎么得到如下所示的重复结果,而我只需要一个结果(WONUM是唯一的):

SELECT
W.WONUM,
W.STATUS,
W.ASSETNUM,
LPAD (' ', 2 * LEVEL - 1) || SYS_CONNECT_BY_PATH (c.CLASSIFICATIONID, '/') PATH
FROM MAXIMO.WORKORDER W
LEFT  JOIN CLASSSTRUCTURE C ON W.CLASSSTRUCTUREID = C.CLASSSTRUCTUREID
WHERE WONUM ='000000004140094'
START WITH c.parent = '1688'
CONNECT BY PRIOR TO_CHAR (c.CLASSSTRUCTUREID) = c.PARENT

我只是想知道我做错了什么,或者我一般都要重写我的查询。

编辑:两个表的结构

工单表:

WONUM       STATUS      ASSETNUM       PATH
000000004140094 COMP        51110          /CM/POSTFAILCM
000000004140094 COMP        51110          /CM/POSTFAILCM
000000004140094 COMP        51110          /CM/POSTFAILCM
000000004140094 COMP        51110          /CM/POSTFAILCM
000000004140094 COMP        51110          /CM/POSTFAILCM
000000004140094 COMP        51110          /CM/POSTFAILCM
000000004140094 COMP        51110          /CM/POSTFAILCM
000000004140094 COMP        51110          /CM/POSTFAILCM

类结构表:

WONUM       STATUS  ASSETNUM    CLASSSTRUCTUREID
000000000108085 CLOSE   00199928    1000
000000000108247 CLOSE   00202763    1061
000000000108248 CLOSE   00202763    1061
000000000108273 CLOSE   00199790    1000

2 个答案:

答案 0 :(得分:1)

一步一步地执行此操作:

  • 从所有根路径开始获取路径层次结构(FLT,FLT / ASSET,FLT / PMFLT,FLT / CM,FLT / CM / POSTFAILCM)(即没有父路径;样本数据中只有FLT)
  • 加入'000000004140094'记录

使用标准SQL的递归WITH子句:

with paths(classstructureid, path) as
(
  select classstructureid, classifcationid as path
  from classstructure 
  where parent is null
  union all
  select c.classstructureid, p.path || '/' || c.classifcationid as path
  from classstructure c
  join paths p on p.classstructureid = c.parent
)
select
  w.wonum,
  w.status,
  w.assetnum,
  p.path
from workorder w
join paths p on p.classstructureid = w.classstructureid
where w.wnum = '000000004140094';

答案 1 :(得分:0)

我认为您的重复问题是指定根

的条件
START WITH c.parent = '1688'

这不是指定根记录,这个条件会匹配来自你的根目录的所有记录(或者是它?)。

要指定根记录,它应该是

START WITH c.classstructureid = '1688'