简体   繁体   English

如何在 SQL 中区分 select 年份?

[英]How to select distinct years in SQL?

SELECT 
    name AS Name,
    SUM(number) AS number_of_names
FROM 
    `bigquery-public-data.usa_names.usa_1910_current` 
WHERE 
    name = 'Jacob'
    AND year > 2003
    AND year < 2008
GROUP BY 
    name

So this is the query I am trying to run but I can't for the life of me figure out to select the specific years all in one query.所以这是我试图运行的查询,但我无法在一个查询中找出 select 的特定年份。 Like the outputs of each year distinctly laid out.就像每年的产出都清楚地列出来。 So the output would be所以 output 将是

2003 Jacob 1203123
2004 Jacob 123123
2005 Jacob 123123
2006 Jacob 1231239
2007 Jacob 123123
2008 Jacob 12312

Data is from here: https://console.cloud.google.com/marketplace/details/social-security-administration/us-names数据来自这里: https://console.cloud.google.com/marketplace/details/social-security-administration/us-names

I think you are looking for something like this.我想你正在寻找这样的东西。 You need to GROUP BY name, year to get a distinct sum for a name for every year.您需要GROUP BY name, year以获得每年的名称的不同总和。 Also, >= 2003 and <= 2008 makes the query inclusive, otherwise it will skip the years 2003 and 2008 respectively.此外, >= 2003<= 2008使查询包含在内,否则它将分别跳过 2003 年和 2008 年。

SELECT 
    name AS Name, 
    year as Year,
    sum (number ) AS number_of_names
FROM `bigquery-public-data.usa_names.usa_1910_current` 
WHERE name = 'Jacob'
AND year >= 2003
AND year <= 2008
GROUP BY name, year
ORDER BY  year

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM