2等于select

时间:2016-07-13 22:37:58

标签: sql postgresql postgresql-9.1

我必须通过 Levenshtein 函数处理我的数据。 在这种情况下,我使用嵌套选择

SELECT levenshtein(a.param, b.param), *
FROM   (
         SELECT 5 fields
         FROM table t,
         JOIN x,
         JOIN y,
         GROUP BY 1, 2, 3
       ) a,
FROM   (
         SELECT 5 fields
         FROM table t,
         JOIN x,
         JOIN y,
         GROUP BY 1, 2, 3
       ) b,

有没有办法不复制内部SELECT

1 个答案:

答案 0 :(得分:0)

解决方案非常简单,感谢@Nikarus关于WITH表达式的建议:

WITH subtable AS (
    SELECT 5 fields
     FROM table t,
     JOIN x,
     JOIN y,
     GROUP BY 1, 2, 3
)
SELECT levenshtein(a.param, b.param), *
FROM subtable a, subtable b