我为这种奇怪的行为而生气。
我正在使用2个SYS_CONNECT_BY_PATH查询,但是在我确认其中有值的同时,总是查询为空。
目标是将多行连接在一起,并以逗号分隔。
这是工作的:
def periods(periods):
total_periods={}
for period in periods:
total_periods[period] = {}
for startdate in periods[period][0]:
total_periods[period]['startdate'] = startdate
for enddate in periods[period][-1]:
total_periods[period]['enddate'] = enddate
通过此查询,内部(在SYS_CONNECT_BY_PATH之前)SQl结果为
SELECT id_audit_req, SUBSTR(MAX(SYS_CONNECT_BY_PATH (profs_names, ', ')), 3) all_descriptions
FROM (SELECT ROW_NUMBER () OVER (PARTITION BY id_audit_req ORDER BY id_audit_req, profs_names) rnum, id_audit_req, profs_names
FROM (SELECT id_audit_req, nvl(p.nick_name, p.name) profs_names
FROM audit_req_prof arp, professional p
WHERE arp.flg_rel_type = 'A'
AND arp.id_professional = p.id_professional
)
)
START WITH rnum = 1
CONNECT BY PRIOR rnum = rnum - 1 AND PRIOR id_audit_req = id_audit_req
GROUP BY id_audit_req
最终结果是正确的:
rnum id_audit_req profs_names
1 1 Auditor
1 501 Auditor
1 502 Auditor
2 502 Auditor2
1 503 Auditor
1 504 Auditor
1 505 Auditor
2 505 Auditor2
现在我有另一个查询,内部结果也正确,但是最终结果为null,我不知道为什么:
id_audit_req all_descriptions
1 Auditor
504 Auditor
502 Auditor, Auditor2
505 Auditor, Auditor2
503 Auditor
501 Auditor
内部结果:
SELECT id_epis_triage, SUBSTR (MAX(SYS_CONNECT_BY_PATH (bp, ', ')), 3) all_descriptions
FROM (SELECT ROW_NUMBER () OVER (PARTITION BY id_epis_triage ORDER BY id_epis_triage, bp desc) rnum, id_epis_triage, bp
FROM (SELECT id_epis_triage, vsr.VALUE as bp
FROM vital_sign_read vsr, vital_sign vs
WHERE vsr.id_epis_triage in (SELECT arpe.id_epis_triage FROM audit_req_prof_epis arpe
WHERE arpe.id_audit_req_prof = 2)
AND vsr.flg_state = 'A'
AND vsr.id_vital_sign IN (SELECT id_vital_sign_detail FROM vital_sign_relation WHERE relation_domain = 'C')
AND vsr.dt_vital_sign_read in
(SELECT vsr2.dt_vital_sign_read
FROM vital_sign_read vsr2
WHERE (vsr2.id_epis_triage, vsr2.dt_vital_sign_read) IN
(SELECT id_epis_triage, MAX(vsr4.dt_vital_sign_read)
FROM alert.vital_sign_read vsr4
WHERE vsr4.id_epis_triage in (SELECT arpe.id_epis_triage FROM audit_req_prof_epis arpe
WHERE arpe.id_audit_req_prof = 2)
AND vsr4.id_vital_sign = vsr.id_vital_sign
AND vsr4.flg_state = 'A'
group by id_epis_triage)
AND vsr2.id_vital_sign = vsr.id_vital_sign
AND vsr2.id_vital_sign IN (SELECT id_vital_sign_detail
FROM vital_sign_relation
WHERE relation_domain = 'C'
AND vsr2.id_epis_triage in (SELECT arpe.id_epis_triage FROM audit_req_prof_epis arpe
WHERE arpe.id_audit_req_prof = 2)
AND id_vital_sign_parent = 28)
AND vsr2.flg_state = 'A')
AND vs.id_vital_sign = vsr.id_vital_sign
ORDER BY vs.intern_name_vital_sign
)
)
START WITH rnum = 1
CONNECT BY PRIOR rnum = rnum - 1 and PRIOR id_epis_triage = id_epis_triage
group by id_epis_triage
最终结果:
空
有人知道吗?我尝试了很多变化,几乎总是相同的结果。 仅当我删除START WITH子句时,我才知道:
rnum id_epis_triage bp
1 2134 120
2 2134 85
1 2137 112
2 2137 98
这似乎是每个rnum仅获得最后一个值。 但是,从本质上讲,两个SQL都是相同的,结构是完全相同的。
答案 0 :(得分:0)
似乎对我有用(我在11.2.0.4上):
WITH inner_result AS (SELECT 1 rnum, 2134 id_epis_triage, 120 bp FROM dual UNION ALL
SELECT 2 rnum, 2134 id_epis_triage, 85 bp FROM dual UNION ALL
SELECT 1 rnum, 2137 id_epis_triage, 112 bp FROM dual UNION ALL
SELECT 2 rnum, 2137 id_epis_triage, 98 bp FROM dual)
SELECT id_epis_triage,
substr(MAX(sys_connect_by_path(bp, ', ')), 3) all_descriptions
FROM inner_result
START WITH rnum = 1
CONNECT BY PRIOR rnum = rnum - 1
AND PRIOR id_epis_triage = id_epis_triage
GROUP BY id_epis_triage;
ID_EPIS_TRIAGE ALL_DESCRIPTIONS
-------------- ----------------
2137 112, 98
2134 120, 85
您使用的是哪个版本,上述查询(按原样,包括inner_result子查询)也对您有用吗?
答案 1 :(得分:0)
所以,最后的问题是...子查询似乎很大,并且SYS_CONNECT_BY_PATH无法正常工作(至少在我的Oracle 10.2版中)。
因此,我对SQL子查询进行了细化,以得到类似的结果,但文本较少,这就是结果:
<div class="amazing">
<div class="good">"good text"</div>
<div class="bad">"bad text"</div>
<div class="worse">"worse text"</div>
</div>
确实是一个奇怪的问题,但是感谢@Boneist,我希望找到这种选择。
SELECT id_epis_triage, SUBSTR (MAX(SYS_CONNECT_BY_PATH (bp, ', ')),3) all_descriptions
FROM (SELECT ROW_NUMBER () OVER (PARTITION BY id_epis_triage ORDER BY id_epis_triage, bp desc) rnum, id_epis_triage, bp
FROM (SELECT id_epis_triage, vsr.VALUE as bp
FROM vital_sign_read vsr, vital_sign vs
WHERE vsr.flg_state = 'A'
AND vsr.id_vital_sign = vsr.id_vital_sign
AND vs.id_vital_sign = vsr.id_vital_sign
AND vsr.id_vital_sign IN (SELECT id_vital_sign_detail
FROM vital_sign_relation
WHERE relation_domain = 'C'
AND id_vital_sign_parent = 28)
AND (vsr.id_epis_triage, vsr.dt_vital_sign_read) IN
(SELECT id_epis_triage, MAX(vsr4.dt_vital_sign_read)
FROM vital_sign_read vsr4
WHERE vsr4.flg_state = 'A'
AND vsr4.id_epis_triage in (SELECT arpe.id_epis_triage FROM audit_req_prof_epis arpe WHERE arpe.id_audit_req_prof = 2)
group by id_epis_triage
)
ORDER BY vs.intern_name_vital_sign
)
)
START WITH rnum = 1
CONNECT BY PRIOR rnum = rnum - 1 and prior id_epis_triage = id_epis_triage
group by id_epis_triage;