我有以下查询:
WITH
curr AS (
SELECT COUNT(*)
FROM table1
WHERE column1 LIKE 'A%'
AND column2 = TRUNC(CURRENT_TIMESTAMP)
),
hist AS (
SELECT COUNT(*)
FROM table2
WHERE column1 LIKE 'A%'
AND column2 = TRUNC(CURRENT_TIMESTAMP)
)
SELECT (curr+hist) FROM dual;
现在此查询不起作用,因为curr
和hist
是无效的标识符,但我想将两个结果合并为一个总数并返回该结果。怎么可以这样做?
答案 0 :(得分:1)
您需要在SELECT
中引用两个CTE。像
WITH
curr AS (
SELECT COUNT(*) cnt
FROM table1
WHERE column1 LIKE 'A%'
AND column2 = TRUNC(CURRENT_TIMESTAMP)
),
hist AS (
SELECT COUNT(*) cnt
FROM table2
WHERE column1 LIKE 'A%'
AND column2 = TRUNC(CURRENT_TIMESTAMP)
)
SELECT curr.cnt + hist.cnt
FROM curr
CROSS JOIN hist
会奏效。
WITH
curr AS (
SELECT COUNT(*) cnt
FROM table1
WHERE column1 LIKE 'A%'
AND column2 = TRUNC(CURRENT_TIMESTAMP)
),
hist AS (
SELECT COUNT(*) cnt
FROM table2
WHERE column1 LIKE 'A%'
AND column2 = TRUNC(CURRENT_TIMESTAMP)
)
SELECT SUM(cnt)
FROM (SELECT curr.cnt FROM curr
UNION ALL
SELECT hist.cnt FROM hist)