我有这个SP,正在尝试在Oracle目标表(11.2)中插入记录。这里在子查询中命名为“ a”,其中我有一个列PROJECT
,这是子查询“ b”的连接条件之一,但是在这里,如果我将列“ project”放入,则无法获得预期的结果在子查询“ a”的group by子句中。
但是同时我需要引用与子查询'b'的连接条件之一相同的内容。
CREATE OR REPLACE PROCEDURE PROC1
AS
BEGIN
INSERT INTO TARGET_TABLE
SELECT a.SECT,
a.REG,
a.USRN,
a.PRO_CT,
a.CATG,
a.TLT,
a.SN_YR_MN,
a.SN_YR,
a.SN_MN,
a.UNIQUE_ISSUES_CNT,
a.FILES_SN_ISS_CT,
b.TOT_FILES_SN_CT,
b.TOT_SN_CT,
CURRENT_DATE
FROM ( SELECT SECT,
REG,
USRN,
PROJECT,
COUNT (DISTINCT CONCAT (PROJECT, SOLUTION)) PRO_CT,
CATG,
TLT,
SN_YR_MN,
SN_YR,
SN_MN,
COUNT (DISTINCT ISSUE_ID) UNIQUE_ISSUES_CNT,
COUNT (DISTINCT PATH) FILES_SN_ISS_CT
FROM table1
GROUP BY SECT,
REG,
PROJECT,
USRN,
CATG,
TLT,
SN_YR_MN,
SN_YR,
SN_MN) a
JOIN
( SELECT USRN,
PROJECT,
SECT,
REG,
SN_YR_MN,
SN_YR,
SN_MN,
COUNT (DISTINCT RPT_SCAN_SUMMARY.PATH) TOT_FILES_SN_CT,
COUNT (DISTINCT RPT_SCAN_SUMMARY.SCAN_ID) TOT_SN_CT
FROM table2
GROUP BY USRN,
PROJECT,
SECT,
REG,
SN_YR_MN,
SN_YR,
PROJECT,
SN_MN) b
ON a.USRN = b.USRN
AND a.SECT = b.SECT
AND a.REG = b.REG
AND a.SN_YR_MN = b.SN_YR_MN
AND a.SN_YR = b.SN_YR
AND a.SN_MN = b.SN_MN
AND a.PROJECT = b.PROJECT;
END;