是否可能有类似的内容:
SELECT MAX(id), somefield
UNION ALL
SELECT MIN(id), someotherfield
FROM
(SOME GIANT SQL JOIN)
基本上,我想在两个UNIONed查询中使用相同的连接子查询。
答案 0 :(得分:3)
您可以这样使用with子句:
with sub as
(some giant sql join)
select max(id), somefield
from sub
group by somefield
union all
select min(id), someotherfield from sub group by someotherfield
(作为两次写出相同内联视图的替代方法)