数据库语言 数据查询_使用这种简单的查询语言开始查询数据

数据库语言 数据查询

Working with data is becoming an increasingly important skill in the modern workplace.

在现代工作场所中,处理数据已成为越来越重要的技能。

Data is no longer the domain of analysts and software engineers. With today's technology, anyone can work with data to analyse trends and inform their decision making.

数据不再是分析师和软件工程师的领域。 借助当今的技术,任何人都可以使用数据来分析趋势并为决策提供依据。

A fundamental concept when working with data is 'querying' a data set. This is to literally ask questions about a set of data. A query language is a software language that provides a syntax for asking such questions.

处理数据时的基本概念是“查询”数据集。 这是从字面上询问有关一组数据的问题。 查询语言是一种软件语言,提供用于询问此类问题的语法。

If you don't have any experience writing queries, they can appear a little intimidating. However, with a little practice, you can master the basics.

如果您没有编写查询的经验,它们可能会显得有些吓人。 但是,只需进行一些练习,即可掌握基础知识。

Here's how you can get started in Google Sheets.

这是您开始使用Google表格的方法

Google可视化API查询语言 (Google Visualization API Query Language)

You may already be using Google Sheets for much of your day-to-day work. Perhaps you are familiar with using it to generate charts and graphs.

您可能已经在大部分日常工作中使用Google表格。 也许您熟悉使用它来生成图表。

The Google Visualization API Query Language is the magic that works behind the scenes to make this possible.

Google Visualization API查询语言是在幕后起作用的魔力,使之成为可能。

But did you know you can access this language through the QUERY() function? It can be a powerful tool for working with large sheets of data.

但是您知道可以通过QUERY()函数访问此语言吗? 它是处理大量数据的强大工具。

There are a lot of similarities between the query language and SQL.

查询语言和SQL之间有很多相似之处。

In both cases, you define a data set of columns and rows, and choose different columns and rows by specifying various criteria and conditions.

在这两种情况下,您都将定义列和行的数据集,并通过指定各种条件和条件来选择不同的列和行。

In this article, the example data will come from a large CSV file containing international football results between 1872 and 2019. You can download the data from Kaggle.

在本文中,示例数据将来自一个大型CSV文件,其中包含1872年至2019年之间的国际足球比赛结果。您可以从Kaggle下载数据

In a new Google Sheet, upload the CSV file. You can select all the data with Ctrl+A (or Cmd+A on Mac).

在新的Google表格中,上传CSV文件。 您可以使用Ctrl + A(在Mac上为Cmd + A)选择所有数据。

From the menu ribbon, choose Data > Named ranges... and call the range selected something like 'data'. This will make it easier to work with.

从功能区菜单中,选择“数据”>“命名范围...”,然后将所选范围称为“数据”。 这将使其更易于使用。

Now, you are ready to start querying the data. Create a new tab in the spreadsheet, and in cell A1, create a new QUERY() formula.

现在,您可以开始查询数据了。 在电子表格中创建一个新选项卡,并在单元格A1中创建一个新的QUERY()公式。

获取所有英格兰比赛 (Get all England matches)

This first query finds all the rows in the data set where England are either the home team or the away team.

第一个查询查找数据集中所有英格兰为主队或客队的行。

The QUERY() formula takes at least two arguments. The first is the named range, which will be the data set queried. The second is a string that contains the actual query.

QUERY()公式至少接受两个参数。 第一个是命名范围,它将是查询的数据集。 第二个是包含实际查询的字符串。

=QUERY(data, "SELECT * WHERE B = 'England' OR C = 'England'")

Let's break this down.

让我们分解一下。

SELECT * asks to return all columns in the data set. If you only wanted columns A, B and C, you would write SELECT A, B, C.

SELECT *要求返回数据集中的所有列。 如果只需要A,B和C列,则可以编写SELECT A, B, C

Next, you include a filter to find only rows where column B or column C contain the team 'England'. Make sure to use single-quotes for strings inside the query. Double-quotes are used to open and close the query itself.

接下来,包括一个过滤器,以仅查找B列或C列包含团队'England' 。 确保查询中的字符串使用单引号。 双引号用于打开和关闭查询本身。

This formula returns all the rows where England have played. If you want to search for another team, simply change the condition in the filter.

此公式将返回英格兰已打过的所有行。 如果要搜索另一个团队,只需在过滤器中更改条件。

计算所有友谊赛 (Count all friendly matches)

Next, let's count how many friendly matches are in the data set.

接下来,让我们计算一下数据集中有多少个友好匹配项。

=QUERY(data, "SELECT COUNT(A) WHERE F = 'Friendly'")

This makes use of the Query Language's COUNT() function. This is an example of an aggregate function. Aggregate functions summarise many rows into one.

这利用了查询语言的COUNT()函数。 这是聚合函数的示例。 聚合函数将许多行汇总为一。

For example, in this data set there are 16,716 rows where column F equals 'Friendly'. Instead of returning all these rows, the query returns a single row - which counts them instead.

例如,在此数据集中,有16,716行,其中列F等于'Friendly' 。 查询不返回所有这些行,而是返回单个行-对其进行计数。

Other examples of aggregate functions include MAX(), MIN() and AVG(). Instead of returning all the rows matching the query, it finds their maximum, minimum and average values instead.

聚合函数的其他示例包括MAX()MIN()AVG() 。 而不是返回与查询匹配的所有行,而是查找它们的最大值,最小值和平均值。

按比赛分组 (Group by tournament)

Aggregate functions can do more if you use a GROUP BY statement alongside them. This query finds out how many matches have been played by each tournament type.

如果在汇总函数旁边使用GROUP BY语句,则汇总函数可以做更多事情。 该查询找出每种锦标赛类型进行了多少场比赛。

=QUERY(data, "SELECT F, COUNT(A) GROUP BY F")

This query groups the data set by each of the values in column F. It then counts how many rows there are in each group.

该查询按F列中的每个值对数据集进行分组。然后它计算每个组中有多少行。

You can use GROUP BY on more than one column. For example, to find how many matches have been played in each country by tournament, use the query below:

您可以在多个列上使用GROUP BY 。 例如,要查找每个国家在锦标赛中进行了多少场比赛,请使用以下查询:

=QUERY(data, "SELECT H, F, COUNT(A) GROUP BY H, F")

Let's try some more advanced filtering.

让我们尝试一些更高级的过滤。

获取所有英格兰vs德国的比赛 (Get all England vs Germany matches)

You can specify more complex filter logic using the AND and OR keywords. For readability, it can help to use brackets around each part of the filter.

您可以使用ANDOR关键字指定更复杂的过滤器逻辑。 为了便于阅读,可以在过滤器的每个部分周围使用方括号。

For example, to find all the matches between England and Germany:

例如,要查找英格兰和德国之间的所有比赛:

=QUERY(data, "SELECT * WHERE (B = 'England' AND C = 'Germany') OR (C = 'England' AND B ='Germany')")

This filter has two criteria - one where England are the home team and Germany are away, and the other vice versa.

此筛选条件有两个条件-一个条件是英格兰是主队,而德国则不在,另一个则相反。

Using data validation makes it easy to pick any two teams in the data set.

使用数据验证可轻松选择数据集中的任何两个团队。

Then, you can write a query that uses the values of different cells in its filter. Remember to use single-quotes for identifying strings within the query, and double-quotes to open and close different pieces of the query.

然后,您可以编写一个查询,该查询使用其过滤器中不同单元格的值。 请记住使用单引号标识查询中的字符串,并使用双引号打开和关闭查询的不同部分。

=QUERY(data, "SELECT * WHERE (B = '"&B1&"' AND C = '"&B2&"') OR (C = '"&B1&"' AND B ='"&B2&"')")

Aggregate functions and filters make powerful tools when used in combination. Once you are comfortable with how they work, you can start searching for all kinds of interesting trends in your data set.

聚合函数和过滤器结合使用时将成为强大的工具。 一旦熟悉了它们的工作方式,就可以开始在数据集中搜索各种有趣的趋势。

For example, the query below finds the average goals per game, by each year since 1900.

例如,下面的查询查找自1900年以来每年的每场比赛平均目标。

=QUERY(data, "SELECT YEAR(A), AVG(D) + AVG(E) WHERE YEAR(A) >= 1900 GROUP BY YEAR(A)")

If you plot the query result as a line graph, you can immediately start seeing trends over time.

如果将查询结果绘制为折线图,则可以立即开始查看一段时间内的趋势。

排序结果 (Ordering the results)

Sometimes, you are not interested in finding all the matching rows in a data set. Often, you will want to sort them according to some criteria. Perhaps you only wish to find the top ten records.

有时,您对查找数据集中的所有匹配行都不感兴趣。 通常,您将需要根据一些条件对它们进行排序。 也许您只希望找到前十个记录。

This query finds the top ten highest scoring matches in the data set.

此查询查找数据集中得分最高的十个匹配项。

=QUERY(data, "SELECT * ORDER BY (D+E) DESC LIMIT 10")

Notice the ORDER BY statement. This sorts the rows according to the columns specified.  Here, the query sorts the output by the number of goals scored in the game.

注意ORDER BY语句。 这将根据指定的列对行进行排序。 在此,查询按游戏中得分的目标数对输出进行排序。

The DESC keyword indicates to sort in descending order (the ASC keyword would have sorted them in ascending order).

DESC关键字指示按降序排序( ASC关键字将按升序对它们排序)。

Finally, the LIMIT keyword restricts the output to a given number of rows (in this case, ten).

最后, LIMIT关键字将输出限制为给定的行数(在本例中为10行)。

Looks like there have been some pretty one-sided games in Oceania!

好像在大洋洲有一些漂亮的单面游戏!

哪些城市举办了最多的世界杯比赛? (Which cities have hosted the most World Cup matches?)

And now for one final example to bring everything together and get your imagination going.

现在,作为最后一个例子,将所有内容整合在一起,激发您的想象力。

This query finds the top ten cities that have hosted the most FIFA World Cup matches.

此查询查找举办最多FIFA世界杯比赛的前十名城市。

=QUERY(data, "SELECT G, COUNT(A) WHERE F = 'FIFA World Cup' GROUP BY G ORDER BY COUNT(A) DESC LIMIT 10")

现在轮到你了 (Now it's your turn)

Hopefully you have found this article useful. If you are comfortable with the logic in each example, then you are ready to try out real SQL.

希望您发现本文有用。 如果您熟悉每个示例中的逻辑,那么您就可以尝试使用实际SQL。

This will introduce concepts such as JOINS, nested queries and WINDOW functions. When you master these, your power to manipulate data will go through the roof.

这将介绍诸如JOINS,嵌套查询和WINDOW函数之类的概念。 当您掌握了这些内容后,您处理数据的能力就会大打折扣。

There are a number of places to start with learning SQL. Try out the interactive examples at w3schools!

有很多地方可以开始学习SQL。 在w3schools上尝试互动示例

翻译自: https://www.freecodecamp.org/news/start-querying-data-with-google-query-language/

数据库语言 数据查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值