研究员, 我有一个查询如下:
SELECT A.ID, B.ID, (HUGE SUBQUERY) as HS
FROM TABLE_A JOIN TABLE_B ON A.ID = B.ID
WHERE (HUGE SUBQUERY) > 0
我想避免重复子查询。 有没有办法重写我的WHERE像
WHERE HS > 0
或者我必须将子查询转换为连接?
答案 0 :(得分:6)
看看With子句:
WITH HS AS (Huge subquery)
SELECT A.ID, B.ID, HS
FROM TABLE_A JOIN TABLE_B ON A.ID = B.ID
WHERE HS > 0
OR
SELECT *
FROM
(
SELECT A.ID, B.ID, (HUGE SUBQUERY) as HS
FROM TABLE_A JOIN TABLE_B ON A.ID = B.ID
)
WHERE HS > 0
答案 1 :(得分:5)
您可以使用CTE:
WITH cteHS AS (
SELECT xxx AS Value
FROM Huge Subquery
)
SELECT A.ID, B.ID, cteHS.Value as HS
FROM TABLE_A, cteHS
JOIN TABLE_B ON A.ID = B.ID
WHERE cteHS.Value > 0