假设您是一名数据分析师,已在 Data Beans 工作了几个月。您已经和团队成员共同完成了几个卓有成效的项目,现在公司给您分派了第一个独立项目。您已经能够独立编写较为复杂的查询,但现在公司要求您构建更复杂的查询,以获取更深入的数据洞见。虽然团队成员都很乐于提供帮助,但您需要开始在工作中展现自己更独立的一面。
Show the menu IDs and total revenue from the order_item table with the top three highest and top three lowest by total revenue.
点击生成。Gemini 将会生成一个类似以下内容的 SQL 语句:
(
SELECT
menu_id,
SUM(item_total) AS total_revenue
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item`
GROUP BY 1
ORDER BY
total_revenue DESC
LIMIT 3
)
UNION ALL
(
SELECT
menu_id,
SUM(item_total) AS total_revenue
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item`
GROUP BY 1
ORDER BY
total_revenue
LIMIT 3
);
点击插入。您生成的查询即会添加到“未命名的查询”标签页中。
解释该查询
选中该查询。
点击查询左侧的 图标。
点击解释此查询。
Gemini 对话框将在 BigQuery Studio 的右侧打开。
点击开始聊天。在聊天窗口中,您将看到类似以下内容的查询解释:
仔细阅读该解释。Gemini 会提供类似以下内容的回答摘要:
In summary, this query helps you identify the most and least popular menu items based on their revenue, providing valuable insights for business decisions.
Join the menu table with the order item table, return the menu_id, the item_name, and show the top three highest items and bottom three lowest items by total_revenue.
点击生成。Gemini 将会生成一个类似以下内容的 SQL 语句:
(
SELECT
t1.menu_id,
t1.item_name,
SUM(t2.item_total) AS total_revenue
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.menu` AS t1
INNER JOIN `{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item` AS t2 ON t1.menu_id = t2.menu_id
GROUP BY 1, 2
ORDER BY
total_revenue DESC
LIMIT 3
)
UNION ALL
(
SELECT
t1.menu_id,
t1.item_name,
SUM(t2.item_total) AS total_revenue
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.menu` AS t1
INNER JOIN `{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item` AS t2 ON t1.menu_id = t2.menu_id
GROUP BY 1, 2
ORDER BY
total_revenue
LIMIT 3
);
点击插入。您生成的查询即会添加到“未命名的查询”标签页中。
解释该查询
选中该查询。
点击查询左侧的 图标。
点击解释此查询。Gemini 聊天窗口中会显示类似以下内容的摘要,来对查询做出解释:
This query provides a quick and easy way to identify the most popular and least popular items on the coffee shop's menu, which can be valuable for making decisions about inventory, pricing, and menu changes.
(
SELECT
t1.menu_id,
t1.item_name,
ROUND(SUM(t2.item_total), 2) AS total_revenue
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.menu` AS t1
INNER JOIN `{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item` AS t2 ON t1.menu_id = t2.menu_id
GROUP BY 1, 2
ORDER BY
total_revenue DESC
LIMIT 3
)
UNION ALL
(
SELECT
t1.menu_id,
t1.item_name,
ROUND(SUM(t2.item_total), 2) AS total_revenue
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.menu` AS t1
INNER JOIN `{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item` AS t2 ON t1.menu_id = t2.menu_id
GROUP BY 1, 2
ORDER BY
total_revenue
LIMIT 3
);
SELECT
oi.menu_id,
m.item_name,
SUM(oi.item_total) AS total_revenue
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item` AS oi
INNER JOIN `{{{project_0.project_id|set at lab start}}}.menu` AS m ON oi.menu_id = m.menu_id
WHERE m.item_size = 'Small'
GROUP BY 1, 2
ORDER BY
total_revenue DESC
LIMIT 10;
SELECT
oi.menu_id,
m.item_name,
SUM(oi.item_total) AS total_revenue
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item` AS oi
INNER JOIN `{{{project_0.project_id|set at lab start}}}.menu` AS m ON oi.menu_id = m.menu_id
WHERE m.item_size = 'Small'
GROUP BY 1, 2
ORDER BY
total_revenue DESC
LIMIT 10;
点击运行。您会发现该查询运行失败了,并显示如下错误:
未找到:在位置 US 中找不到数据集 :
利用 Gemini 和错误信息来调试代码
打开 BigQuery 中的 Gemini 聊天窗口。
在聊天窗口中,输入如下问题:
Why am I getting "Not found: : was not found in location US" when I run this query?
根据这些建议,您可以判断造成问题的原因很可能是该查询中针对 menu 表的 INNER JOIN 语句没有指定正确的数据集名称 coffee_on_wheels。
建议中提供了类似以下内容的优化查询,有可能可以解决问题:
SELECT
oi.menu_id,
m.item_name,
SUM(oi.item_total) AS total_revenue
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item` AS oi
INNER JOIN `{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.menu` AS m ON oi.menu_id = m.menu_id
WHERE m.item_size = 'Small'
GROUP BY 1, 2
ORDER BY
total_revenue DESC
LIMIT 10;
Can you refactor the code so that we round to only two decimal places displayed with the total_revenue field in the results?
检查您的提示是否如下所示:
I need help refining this query. SELECT oi.menu_id, m.item_name, SUM(oi.item_total) AS total_revenue FROM `.coffee_on_wheels.order_item` AS oi INNER JOIN `.coffee_on_wheels.menu` AS m ON oi.menu_id = m.menu_id WHERE m.item_size = 'Small' GROUP BY 1, 2 ORDER BY total_revenue DESC LIMIT 10; Can you refactor the code so that we round to only two decimal places displayed with the total_revenue field in the results?
点击 。Gemini 给出了如下回答:
SELECT
oi.menu_id,
m.item_name,
ROUND(SUM(oi.item_total), 2) AS total_revenue -- Round to 2 decimal places
FROM
`{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.order_item` AS oi
INNER JOIN `{{{project_0.project_id|set at lab start}}}.coffee_on_wheels.menu` AS m ON oi.menu_id = m.menu_id
WHERE m.item_size = 'Small'
GROUP BY 1, 2
ORDER BY
total_revenue DESC
LIMIT 10;
您将看到类似以下内容的解释:
To limit the total_revenue field to two decimal places, you can use the ROUND() function in BigQuery.
ROUND(SUM(oi.item_total), 2) : This part of the query uses the ROUND() function to round the sum of oi.item_total to two decimal places. The 2 inside the ROUND() function specifies the number of decimal places to keep.
…可帮助您充分利用 Google Cloud 技术。我们的课程会讲解各项技能与最佳实践,可帮助您迅速上手使用并继续学习更深入的知识。我们提供从基础到高级的全方位培训,并有点播、直播和虚拟三种方式选择,让您可以按照自己的日程安排学习时间。各项认证可以帮助您核实并证明您在 Google Cloud 技术方面的技能与专业知识。
本手册的最后更新时间:2024 年 11 月 6 日
本实验的最后测试时间:2024 年 11 月 6 日
版权所有 2025 Google LLC 保留所有权利。Google 和 Google 徽标是 Google LLC 的商标。其他所有公司名和产品名可能是其各自相关公司的商标。