Oracle SQL - 根据数量列生成行

Oracle SQL - Generate rows based on quantity column

我们使用分配了最大人数的合并职位,我需要构建一个报告,为每个负责人创建一行,包括现任者的详细信息(如果有一个)或空缺的空行。

像这样:

Position_Title Headcount Incumbent
Analyst 3 Employee1
Analyst 3 Employee2
Analyst 3

我可以加入 Person/Assignment tables 与位置 table 生成一个单独的行,其中有一个现任者但我正在努力的部分正在生成一个有空缺的行。

我在这里发现另一个 建议使用 connect by 但我无法让它工作。

它似乎是这样独立工作的:

SELECT

HAP.NAME POSITION_TITLE,
HAP.POSITION_CODE,
HAP.ACTIVE_STATUS,
HAP.POSITION_TYPE,
HAP.FTE,
LEVEL row_num

FROM

HR_ALL_POSITIONS_F_VL HAP


CONNECT BY LEVEL <= HAP.FTE
AND PRIOR HAP.POSITION_ID = HAP.POSITION_ID
AND PRIOR sys_guid() IS NOT NULL

但我不确定如何将它用于我的其余查询(我尝试在 CONNECT BY 之前和之后使用 WHERE 子句,但无论哪种方式都超时)

SELECT 
HAP.NAME POSITION_TITLE,
HAP.POSITION_CODE,
PGF.NAME GRADE_NAME,
PGF.GRADE_CODE,
HAP.ACTIVE_STATUS,
HAP.POSITION_TYPE,
HAP.HEADCOUNT,
PAAM.ASSIGNMENT_NUMBER,
LEVEL row_num

FROM
HR_ALL_POSITIONS_F_VL HAP, 
PER_GRADES_F_VL PGF,
PER_ALL_ASSIGNMENTS_M PAAM

WHERE 

    HAP.ENTRY_GRADE_ID = PGF.GRADE_ID
AND PAAM.POSITION_ID(+) = HAP.POSITION_ID
AND      TRUNC(Sysdate) between HAP.effective_start_date AND HAP.effective_end_date
AND      TRUNC(Sysdate) between PGF.effective_start_date AND PGF.effective_end_date
AND      PAAM.effective_start_date(+) <= TRUNC(Sysdate)
AND      PAAM.effective_end_date(+) >= TRUNC(Sysdate)

CONNECT BY LEVEL <= HAP.HEADCOUNT
AND PRIOR HAP.POSITION_ID = HAP.POSITION_ID
AND PRIOR sys_guid() IS NOT NULL

您可以使用 multiset 根据 headcount 列生成行,如下所示:

SELECT 
HAP.NAME POSITION_TITLE,
HAP.POSITION_CODE,
PGF.NAME GRADE_NAME,
PGF.GRADE_CODE,
HAP.ACTIVE_STATUS,
HAP.POSITION_TYPE,
HAP.HEADCOUNT,
PAAM.ASSIGNMENT_NUMBER,
Lvls.Column_value row_num

FROM
HR_ALL_POSITIONS_F_VL HAP, 
PER_GRADES_F_VL PGF,
PER_ALL_ASSIGNMENTS_M PAAM,
table(cast(multiset(select level from dual connect by  level <= hap.headcount) as sys.OdciNumberList)) lvls
WHERE 

    HAP.ENTRY_GRADE_ID = PGF.GRADE_ID
AND PAAM.POSITION_ID(+) = HAP.POSITION_ID
AND      TRUNC(Sysdate) between HAP.effective_start_date AND HAP.effective_end_date
AND      TRUNC(Sysdate) between PGF.effective_start_date AND PGF.effective_end_date
AND      PAAM.effective_start_date(+) <= TRUNC(Sysdate)
AND      PAAM.effective_end_date(+) >= TRUNC(Sysdate);

注意:根据您的要求添加条件并始终使用标准 ANSI 连接。