【发布时间】:2021-10-04 01:39:04
【问题描述】:
所以...我目前使用的是 Oracle 11.1g,我需要创建一个查询,该查询使用来自 Table_with_value 的 ID 和 CusCODE,并使用 ID 检查 Table_with_status 以查找活动的 CO_status 但在不同的 CusCODE 上。
这是我目前所拥有的 - 除非手动提供 CusCODE 和 ID,否则显然不能正常工作:
SELECT wm_concat(CoID) as active_CO_Status_for_same_ID_but_different_CusCODE
FROM Table_with_status
WHERE
CoID IN (SELECT CoID FROM Table_with_status WHERE ID = Table_with_value.ID AND CusCODE != Table_with_value.CusCODE)) AND Co_status = 'active';
Table_with_value:
|CoID | CusCODE | ID | Value |
|--------|---------|----------|----|
|354223 | 1.432 | 0784296L | 99 |
|321232 | 4.212321.22 | 0432296L | 32 |
|938421 | 3.213 | 0021321L | 93 |
Table_with_status:
|CoID | CusCODE | ID | Co_status|
|--------|--------------|----------|--------|
|354223 | 1.432 | 0784296L | active|
|354232 | 1.432 | 0784296L | inactive |
|666698 | 1.47621 | 0784296L | active |
|666700 | 1.5217 | 0784296L | active |
|938421 | 3.213 | 0021321L | active |
|938422 | 3.213 | 0021321L | active |
|938423 | 3.213 | 0021321L | active |
|321232 | 4.212321.22 | 0432296L | active |
|321232 | 4.212321.22 | 0432296L | active |
|321232 | 1.689 | 0432296L | inactive |
预期输出:
|CoID | active_CO_Status_for_same_ID_but_different_CusCODE | ID | Value |
|--------|---------|----------|----|
|354223 | 666698,666700 | 1.432 | 0784296L | 99 |
|321232 | N/A | 4.212321.22 | 0432296L | 32 |
|938421 | N/A | 3.213 | 0021321L | 93 |
关于如何在没有任何 PL/SQL for 循环的情况下理想地实现这一点的任何想法,但它也应该没问题,因为预计输出数据集
对于我提出问题的含糊性质,我提前道歉 :) 如果有不清楚的地方,请告诉我。
【问题讨论】:
-
“显然不起作用” - 为什么很明显;你现在得到什么结果?你也许只需要
group by ID, CusCode?CoId, Value可能是预期的输出(并在选择列表中包含相关列)? -
在选择列表中添加 wm_concat(Table_with_status.CoID) 和 Table_with_value.CoID 是我真正需要帮助的 - 我构造查询的方式是不允许的。
标签: sql oracle11g string-aggregation