【问题标题】:Combine SQL Queries in Same Table在同一个表中合并 SQL 查询
【发布时间】:2021-03-17 15:44:46
【问题描述】:

我正在尝试组合以下查询:

SELECT country_name, 
       avg(value) as Entrance_Age 
FROM `bigquery-public-data.world_bank_intl_education.international_education` 
WHERE indicator_code = 'UIS.THAGE.0' 
GROUP BY country_name
ORDER BY avg(value) DESC LIMIT 10

SELECT avg(value) as Illiterate
FROM `bigquery-public-data.world_bank_intl_education.international_education`
WHERE indicator_code = 'UIS.ILLPOP.AG25T64'
GROUP BY country_name
ORDER BY avg(value) DESC LIMIT 10

第一个查询的输出是:[1]:https://i.stack.imgur.com/jsxx7.png

我们的目标是在 Entrance_Age 旁边添加另一个名为“Illiterate”的列。我试图在入学年龄列旁边显示这 10 个国家中每个国家的文盲率。所有数据都来自同一张表。这些值与指标代码相关联,指标代码是基于指标代码的统计信息。

我尝试了多个连接,但似乎无法找到一个有效的连接。

如果我的问题有什么遗漏,请告诉我。

【问题讨论】:

  • 嗨,Aly,不要解释你的目标,解释你想要输出的内容。你想查询显示你两个结果连续吗?或者你想加入它并根据你指定的条件订购

标签: sql join google-bigquery


【解决方案1】:

您可以使用条件聚合:

SELECT country_name, 
       avg(case when indicator_code = 'UIS.THAGE.0' then value end) as Entrance_Age,
       avg(case when indicator_code = 'UIS.ILLPOP.AG25T64' then value end) as illiterate 
FROM `bigquery-public-data.world_bank_intl_education.international_education` 
GROUP BY country_name
ORDER BY Entrance_Age DESC LIMIT 10

【讨论】:

  • 谢谢你,戈登!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 2017-05-18
相关资源
最近更新 更多