封闭表如何与子查询一起使用groupy by?

时间:2019-05-13 04:14:12

标签: sql

我不知道如何通过子查询的列来group by

我要按language分组,如下所示:

enter image description here

这是我的代码:

select a.name, count(a.language) as count
from
    (select 
        temp2.name,
        countrylanguage.language
    from 
        countrylanguage
    right join  
        temp2
    on 
        temp2.code = countrylanguage.countrycode
    ) as a
group by a.language;

已编辑

我得到了如下解决方案:

select temp2.name, count(countrylanguage.language) 
from countrylanguage 
join temp2 on temp2.code = countrylanguage.countrycode 
group by temp2.name;

2 个答案:

答案 0 :(得分:2)

您可以在下面尝试-不需要任何子查询

select temp2.name,count(countrylanguage.language)
  from countrylanguage join temp2
    on temp2.code = countrylanguage.countrycode
group by temp2.name

答案 1 :(得分:2)

表格数据: 选择*来自国家语言

enter image description here


选择*从temp2

enter image description here

选择a。[名称],SUM(当a。[语言]为NULL则为0,否则为1结束) 从 (选择     temp2。[名称],     国家语言。[语言] 从temp2 左加入countrylanguage ON temp2.​​code = countrylanguage.countrycode)a

按名称分组

enter image description here