为什么在同一个表上使用不同的组和计数的3个内部联接不能使用sqlite?
我有一个包含历史迁移数据的表。它包含移民来自的国家、地区和城镇的字段。我想得到一个表格,显示每个城镇有100个或更多数据集条目的条目数,以及提及的地区和国家,以及该地区和国家的数据集条目数 如果我按城镇进行简单分组,则地区和国家/地区的计数将始终在一个城镇的组内,因此我无法计算地区和国家/地区的条目总数为什么在同一个表上使用不同的组和计数的3个内部联接不能使用sqlite?,sql,sqlite,join,count,syntax-error,Sql,Sqlite,Join,Count,Syntax Error,我有一个包含历史迁移数据的表。它包含移民来自的国家、地区和城镇的字段。我想得到一个表格,显示每个城镇有100个或更多数据集条目的条目数,以及提及的地区和国家,以及该地区和国家的数据集条目数 如果我按城镇进行简单分组,则地区和国家/地区的计数将始终在一个城镇的组内,因此我无法计算地区和国家/地区的条目总数 country | sum_country | region | sum_region | town | sum_town Germany | 324,213 | Prussia
country | sum_country | region | sum_region | town | sum_town
Germany | 324,213 | Prussia | 324,213 | Berlin | 324,213
因此,我尝试使用不同分组的内部联接,并对相同的数据表进行计数(使用不同的别名)
这在国家和地区的两个内部连接中运行良好
country | sum_country | region | sum_region | town | sum_town
Germany | 4,546,321 | Prussia | 864,345 | Berlin | 324,213
Germany | 4,546,321 | Unknown | 3,845,321 | Berlin | 640,139
结果我发现我有很多城镇在不同的国家或地区背景下被提及(因为历史数据不太一致)。为了了解这种现象的数量,我想集成第三个内部连接,以计算任何国家/地区/城镇群中每个城镇的条目总数。结果应该如下所示:
country | sum_country | region | sum_region | town | sum_town | sum_total_town
Germany | 4,546,321 | Prussia | 864,345 | Berlin | 324,213 | 964,352
Germany | 4,546,321 | Unknown | 3,845,321 | Berlin | 640,139 | 964,352
在DB Browser for SQLite中尝试此查询时,我收到一条错误消息,上面说:
'near "(": syntax error:'
以下是我的代码,它不起作用:
select a.country, c.summe_land, a.last_province, e.summe_region, a.last_town, g.summe_stadt count (a.last_town) as summe_kombi
from migrant_data as a
inner join
(select b.country, count(b.country) as summe_land
from migrant_data as b group by b.country) as c
on a.country = c.country
inner join
(select d.last_province, count(d.last_province) as summe_region
from migrant_data as d group by d.last_province) as e
on a.last_province = e.last_province
inner join
(select f.last_town, count(f.last_town) as summe_stadt
from migrant_data as f group by f.last_town) as g
on a.last_town = g.last_town
group by a.country, c.summe_land, a.last_province, e.summe_region, a.last_town, g.summe_stadt
having count(a.last_town) >99
order by c.summe_land desc, e.summe_region desc, count (a.last_town) desc;
我仔细检查了我的第三个内部连接,但它的结构与前两个内部连接完全相同,工作正常。我不知道我应该或不应该把括号放在哪里,也不知道所用括号旁边有什么问题
所以我不知道第三个内部连接有什么问题。我在stackoverflow上找到了各种使用三个内部联接的示例,因此似乎可以聚合三个内部联接。也许我忽略了一些非常简单的事情,我对SQL查询相对来说是新手(实际上我是历史学家)
有什么建议吗?谢谢你的关注 你在计数前漏掉了一个逗号(a.last_town) 试用
select
a.country
, c.summe_land
, a.last_province
, e.summe_region
, a.last_town
, g.summe_stadt
, count (a.last_town) as summe_kombi
from migrant_data as a
inner join (
select b.country, count(b.country) as summe_land
from migrant_data as b group by b.country) as c on a.country = c.country
inner join (
select d.last_province, count(d.last_province) as summe_region
from migrant_data as d
group by d.last_province) as e on a.last_province = e.last_province
inner join (
select f.last_town, count(f.last_town) as summe_stadt
from migrant_data as f
group by f.last_town) as g on a.last_town = g.last_town
group by a.country, c.summe_land, a.last_province, e.summe_region, a.last_town, g.summe_stadt
having count(a.last_town) >99
order by c.summe_land desc, e.summe_region desc, count(a.last_town) desc;
谢谢你的快速回复。逗号起了作用。感谢您提出更好地编写命令的建议,舒尔帮助您查找缺失的逗号。我想我必须找到一个更好地支持sql语法的编辑器。
select
a.country
, c.summe_land
, a.last_province
, e.summe_region
, a.last_town
, g.summe_stadt count(a.last_town) as summe_kombi
^^^ here
from migrant_data as a
...
select
a.country
, c.summe_land
, a.last_province
, e.summe_region
, a.last_town
, g.summe_stadt
, count (a.last_town) as summe_kombi
from migrant_data as a
inner join (
select b.country, count(b.country) as summe_land
from migrant_data as b group by b.country) as c on a.country = c.country
inner join (
select d.last_province, count(d.last_province) as summe_region
from migrant_data as d
group by d.last_province) as e on a.last_province = e.last_province
inner join (
select f.last_town, count(f.last_town) as summe_stadt
from migrant_data as f
group by f.last_town) as g on a.last_town = g.last_town
group by a.country, c.summe_land, a.last_province, e.summe_region, a.last_town, g.summe_stadt
having count(a.last_town) >99
order by c.summe_land desc, e.summe_region desc, count(a.last_town) desc;