在GROUP BY子句中包含子查询

时间:2018-06-27 11:10:40

标签: sql group-by subquery

我想在下面使用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

1 个答案:

答案 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;