因此,我试图从一个州获得一个值,即一个名称的最大数量,然后从所有州获得该名称的总数。我认为我必须执行WHERE * IN
,但不知道该放在哪里。这是我到目前为止的内容
SELECT name,SUM(number) as total
FROM
`bigquery-public-data.usa_names.usa_1910_current`
where state = 'AL'
group by name;
考虑:
select name, sum(number) total
from bigquery-public-data.usa_names.usa_1910_current t
where name = (
select name
from bigquery-public-data.usa_names.usa_1910_current
where state = 'AL'
order by number desc
limit 1
)
group by name
子查询为状态AL恢复具有最大name
的number
。然后,外部查询将在整个数据集中计算该名称的总和number
。
下面是BigQuery标准SQL的内容>>
#standardSQL SELECT name, SUM(IF(state = 'AL', number, 0)) count_in_AL, SUM(number) total_count FROM `bigquery-public-data.usa_names.usa_1910_current` GROUP BY name ORDER BY count_in_AL DESC LIMIT 1
有结果
Row name count_in_AL total_count 1 James 158827 5015584
这意味着:詹姆斯是AL(158,827)中最受欢迎的名字,所有州的总数为5,015,584