在这里,我需要避免group by子句中的这两列,因为它比我的期望值返回更多的记录。
实际上,来自st.cnt
的查询结果返回3条记录,但是当我们以SP
运行时,得到的记录是预期的两倍。
st_cnt.COUNT_FILES,
st_cnt.COUNT_SCANS,
SELECT table1.DA_SEC,
table1.DA_REG,
table1.PROJ,
table1.SOL,
table1.SCAN_YEAR_MON,
to_char(TO_DATE(table1.SCAN_YEAR_MON, 'YYYY-MM'), 'YYYY'),
to_char(TO_DATE(table1.SCAN_YEAR_MON, 'YYYY-MM'), 'MM'),
COUNT(DISTINCT table1.ISSUE_ID),
COUNT(DISTINCT table1.USERNAME),
COUNT(DISTINCT table1.PATH),
st_cnt.COUNT_FILES,
st_cnt.COUNT_SCANS,
sum(unq_Pro.HIGH_CNT),
sum(unq_Pro.MEDIUM_CNT),
sum(unq_Pro.LOW_CNT),
sum(SUPPR_HIGH) - sum(UNSUPPR_HIGH),
sum(SUPPR_MEDIUM) - sum(UNSUPPR_MEDIUM),
sum(SUPPR_LOW) - sum(UNSUPPR_LOW),
CURRENT_DATE
FROM table1
JOIN
(SELECT PROJ, SOL, SCAN_YEAR, SCAN_MONTH, COUNT(DISTINCT PATH) COUNT_FILES, COUNT(DISTINCT SCAN_ID) COUNT_SCANS FROM table2 GROUP BY PROJ, SOL, SCAN_YEAR, SCAN_MONTH) st_cnt
ON st_cnt.PROJ = table1.PROJ--AND st_cnt.SOL = table1.SOL
GROUP BY table1.DA_SEC, table1.DA_REG, table1.PROJ, table1.SOL, table1.SCAN_YEAR_MON, st_cnt.COUNT_FILES, st_cnt.COUNT_SCAN;
答案 0 :(得分:0)
在这种情况下,使用With子句的大量时间确实很有帮助。这是讨论With子句的ask tom线程 Ask Tom Link 也是在Sub Query Factoring
上使用with子句进行子查询分解的有用链接在这种情况下,它还可以帮助我们了解表1的数据,以便我们测试答案。但是,通过您共享的信息,这样可以帮助您
WITH TAB1_DATA AS (
SELECT table1.DA_SEC,
table1.DA_REG,
table1.PROJ,
table1.SOL,
table1.SCAN_YEAR_MON,
to_char(TO_DATE(table1.SCAN_YEAR_MON, 'YYYY-MM'), 'YYYY'),
to_char(TO_DATE(table1.SCAN_YEAR_MON, 'YYYY-MM'), 'MM'),
COUNT(DISTINCT table1.ISSUE_ID),
COUNT(DISTINCT table1.USERNAME),
COUNT(DISTINCT table1.PATH),
sum(unq_Pro.HIGH_CNT),
sum(unq_Pro.MEDIUM_CNT),
sum(unq_Pro.LOW_CNT),
sum(SUPPR_HIGH) - sum(UNSUPPR_HIGH),
sum(SUPPR_MEDIUM) - sum(UNSUPPR_MEDIUM),
sum(SUPPR_LOW) - sum(UNSUPPR_LOW),
CURRENT_DATE
FROM table1
GROUP BY table1.DA_SEC, table1.DA_REG, table1.PROJ, table1.SOL, table1.SCAN_YEAR_MON
)
SELECT TD.* , st_cnt.COUNT_FILES,st_cnt.COUNT_SCANS FROM TAB1_DATA TD ,
(SELECT PROJ, SOL, SCAN_YEAR, SCAN_MONTH, COUNT(DISTINCT PATH) COUNT_FILES,
COUNT(DISTINCT SCAN_ID) COUNT_SCANS FROM table2 GROUP BY PROJ, SOL, SCAN_YEAR,
SCAN_MONTH) st_cnt
WHERE st_cnt.PROJ = TD.PROJ;