为什么我没有单组组功能

时间:2015-04-20 21:22:21

标签: sql subquery

如果这个国家/地区的语言超过3种,我想获得姓名和语言编号。但我不知道为什么我没有单组的功能'错误。

SELECT C.NAME,COUNT(L.LANGUAGE)
FROM WORLD.CITY C
INNER JOIN WORLD.COUNTRYLANGUAGE L ON C.COUNTRYCODE = L.COUNTRYCODE
WHERE C.COUNTRYCODE = (
    SELECT COUNTRYCODE
    FROM WORLD.COUNTRYLANGUAGE
    GROUP BY COUNTRYCODE HAVING COUNT(LANGUAGE) >3)
GROUP BY NAME;
DESC WORLD.CITY;

 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                    NOT NULL NUMBER(10)
 NAME                      NOT NULL CHAR(35 CHAR)
 COUNTRYCODE                   NOT NULL CHAR(3 CHAR)
 DISTRICT                  NOT NULL CHAR(20 CHAR)
 POPULATION                NOT NULL NUMBER(10)
DESC WORLD.COUNTRYLANGUAGE;

 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 COUNTRYCODE                   NOT NULL CHAR(3 CHAR)
 LANGUAGE                  NOT NULL CHAR(30 CHAR)
 ISOFFICIAL                NOT NULL VARCHAR2(4000 CHAR)
 PERCENTAGE                NOT NULL FLOAT(126)

2 个答案:

答案 0 :(得分:0)

您可以重写为:

SELECT C.NAME,COUNT(L.LANGUAGE)
FROM WORLD.CITY C
INNER JOIN WORLD.COUNTRYLANGUAGE L ON C.COUNTRYCODE = L.COUNTRYCODE
GROUP BY C.NAME
HAVING COUNT(L.LANGUAGE) > 3

答案 1 :(得分:-1)

我认为您需要 IN ,当您通过所有选择元素进行分组时,必须成为group by子句的一部分

SELECT C.NAME,COUNT(L.LANGUAGE)
FROM WORLD.CITY C
INNER JOIN WORLD.COUNTRYLANGUAGE L ON C.COUNTRYCODE = L.COUNTRYCODE
WHERE C.COUNTRYCODE IN (
    SELECT COUNTRYCODE
    FROM WORLD.COUNTRYLANGUAGE
    GROUP BY COUNTRYCODE HAVING COUNT(LANGUAGE) >3)
GROUP BY NAME,COUNT(L.LANGUAGE);