DB2/400 分层查询,仅具有父级 where 子句和父子排序
我有一个在 IBMi(AS/400、iSeries)上运行的旧版 DB2/400 DB。我正在尝试查询一个引用自身的表,该表模拟了由“案例”组成的帮助台系统。案例可能有子案例(但子案例可能没有)。每个案例都有一个唯一的 ID 和一个父 ID(以及许多其他字段)。如果这是父项,则父项 ID 字段为空(有时但并非总是它的值为 *PARENT);如果它是子项,则它具有父项的 ID。大多数病例没有孩子,有些病例有多个孩子(不允许有孙子)。
除了我需要子案例(如果有)跟随其父案例之外,顺序并不那么重要。我还需要在父母上设置一个 where 子句,仅按日期和状态进行选择。如果父级是 - 其中仅适用于父级,则应始终包含父级的子级。孩子们的顺序并不重要。
我已经成功地选择了父级记录,然后是子级(如果有),但未能仅将 where 应用于父级。如果我应用下面示例中的 where 那么它适用于父级和子级:
SELECT id, parent, LEVEL AS level
FROM cases
/*where ... */
CONNECT BY parent = prior id
order siblings by id;
我只需要它适用于父级。
我也尝试过使用递归公用表表达式,但我要么没有得到父母,要么得到了循环错误。我使用的测试数据库表不是很干净,并且没有强制执行规则的约束。
许多 RCTE 示例都具有通过各种记录进行链接并定义起点的想法。我没有起点,我只想使用特定条件选择父记录,然后包含其所有子记录。
非常感谢任何帮助。
案例表示例:
ID Parent ID
-----------------
P1234
P1235
P1236
P1237
P1238
P1239
C993 P1235
C994 P1235
C995 P1238
C996 P1235
预期结果排序(父母之内/之下的孩子):
P1234
P1235
C993 P1235
C994 P1235
C996 P1235
P1236
P1237
P1238
C995 P1238
P1239
谢谢。
I have a legacy DB2/400 DB running on IBMi (AS/400, iSeries). I am trying to query a table which refers to itself which models a help desk system consisting of 'cases'. Cases may have child cases (but child cases may not). Each case has a unique ID and also a parent ID (as well as lots of other fields). The parent ID field is blank if this is the parent (sometimes but not always it has a value of *PARENT), if it is a child it has the ID of the parent. Most cases do not have children, some cases have mulitple children (grandchildren not allowed).
The ordering is not so important except that I need child cases (if any) to follow their parent. I also need to set a where clause on the parents only to select by date and status. The children of the parent should always be included if the parent is - the where only applies to the parent. The ordering of the chilren is not important.
I have been successful in selecting the records with the parent followed by the children (if any) but not in applying the where to the parent only. If I apply the where as in the example below then it applies to the parent AND the children:
SELECT id, parent, LEVEL AS level
FROM cases
/*where ... */
CONNECT BY parent = prior id
order siblings by id;
I just need it to apply to the parent.
I have also tried using recursive common table expressions but I either didn't get the parents or got a cycle error. The test DB table I am using is not very clean and there are no constraints to enforce the rules.
Many of the RCTE examples have the idea of links through various records and defining a starting point. I don't have a starting point, I just want to select the parent records using certain criteria, then include all their children.
Any help much appreciated.
Example Cases table:
ID Parent ID
-----------------
P1234
P1235
P1236
P1237
P1238
P1239
C993 P1235
C994 P1235
C995 P1238
C996 P1235
Expected result ordering (children within/below parents):
P1234
P1235
C993 P1235
C994 P1235
C996 P1235
P1236
P1237
P1238
C995 P1238
P1239
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明:我没有机会在 DB2-400 中测试下面所示的查询,因此我在 DB2-LUW 中展示一个示例。如果 AS/400 未完全接受该语法,则查询可能需要进行一些调整。
您可以执行以下操作:
结果:
请参阅 db<>fiddle 处的运行示例。
Disclaimer: I don't have the chance of testing the query shown below in DB2-400, so I'm showing an example in DB2-LUW. Maybe the query could require some tweaks if the syntax is not fully accepted in the AS/400.
You can do:
Result:
See running example at db<>fiddle.