我想在下面使用GROUP BY聚合我的SQL查询,但是我不确定如何将内部SELECT包含到GROUP BY子句中。是否可以像GROUP BY PEOPLE.NAME, PEOPLE.ADDRESS, CITY
那样做?
SELECT PEOPLE.NAME AS NAME,
PEOPLE.ADDRESS AS ADDRESS,
(SELECT PEOPLE.CITY
FROM ...
WHERE ...) AS CITY
FROM MYSCHEMA.PEOPLE PEOPLE
GROUP BY PEOPLE.NAME, PEOPLE.ADDRESS
答案 0 :(得分:1)
使用JOIN
:
SELECT p.NAME AS NAME, p.ADDRESS AS ADDRESS, c.CITY
FROM MYSCHEMA.PEOPLE p LEFT JOIN
(SELECT . . .
FROM ...
WHERE ...
) c
ON . . .
GROUP BY p.NAME, p.ADDRESS, c.CITY;