[英](SQL) How do you select a max float value along with other datatypes values within a query?
I'm working with the Iowa Liquor Sales dataset which in this case is called "bigquery-public-data.iowa_liquor_sales.sales".我正在使用 Iowa Liquor Sales 数据集,在本例中称为“bigquery-public-data.iowa_liquor_sales.sales”。 Relevant columns and their datatypes are date(DATE), sale_dollars(FLOAT), item_description(STRING), store_name(STRING).相关列及其数据类型为 date(DATE)、sale_dollars(FLOAT)、item_description(STRING)、store_name(STRING)。
I am trying to write a query that will return the top sale for each year, of the past three years (2021,2020,2019) along with the date, item_description, and store_name.我正在尝试编写一个查询,该查询将返回过去三年(2021、2020、2019)中每年的最高销售额以及日期、item_description 和 store_name。
The below code works, but only covers one year.以下代码有效,但仅涵盖一年。 I know I could copy+paste and change the date every time but that seems tedious.我知道我每次都可以复制+粘贴并更改日期,但这似乎很乏味。 Is there a better way?有没有更好的办法?
SELECT date, sale_dollars, item_description, store_name
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date between '2021-01-01' and '2021-12-31'
ORDER BY sale_dollars DESC
LIMIT 1
date日期 | sale_dollars sale_dollars | item_description商品描述 | store_name商店名称 |
---|---|---|---|
2021-04-19 2021-04-19 | 250932.0 250932.0 | Titos Handmade Vodka铁托斯手工伏特加 | Hy-Vee #3海维 #3 |
When trying different ways to write it so the max sale of 2019,2020, and 2021 return along with their date, item_description, and store_name, I ran into errors.在尝试不同的方式来编写它以使 2019、2020 和 2021 年的最大销售额连同它们的日期、item_description 和 store_name 一起返回时,我遇到了错误。 The below is the closest I got (missing date, item_description, and store_name).下面是我得到的最接近的(缺少日期、item_description 和 store_name)。
SELECT
(SELECT MAX(sale_dollars)
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date between '2021-01-01' and '2021-12-31') as sale_2021,
(SELECT MAX(sale_dollars)
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date between '2020-01-01' and '2020-12-31') as sale_2020,
(SELECT MAX(sale_dollars)
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date between '2019-01-01' and '2019-12-31') as sale_2019
How can I write a query that returns the max sale of the past three years along with it's date, item, and store name?如何编写一个查询来返回过去三年的最大销售额以及日期、商品和商店名称?
As the three values deliver only one value, you can add them to the first query, only adapted to three years由于三个值只传递一个值,您可以将它们添加到第一个查询中,仅适用于三年
SELECT
date, sale_dollars, item_description, store_name,
(SELECT MAX(sale_dollars)
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date between '2021-01-01' and '2021-12-31') as sale_2021,
(SELECT MAX(sale_dollars)
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date between '2020-01-01' and '2020-12-31') as sale_2020,
(SELECT MAX(sale_dollars)
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date between '2019-01-01' and '2019-12-31') as sale_2019
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date between '2019-01-01' and '2021-12-31'
ORDER BY sale_dollars DESC
LIMIT 1
Consider below query考虑以下查询
SELECT EXTRACT(YEAR FROM date) year,
ARRAY_AGG(
STRUCT(date, sale_dollars, item_description, store_name)
ORDER BY sale_dollars DESC LIMIT 1
)[OFFSET(0)].*
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date BETWEEN '2019-01-01' AND '2021-12-31'
GROUP BY 1;
+------+------------+--------------+----------------------+-------------------------------+
| year | date | sale_dollars | item_description | store_name |
+------+------------+--------------+----------------------+-------------------------------+
| 2020 | 2020-10-08 | 250932.0 | Titos Handmade Vodka | Hy-Vee #3 / BDI / Des Moines |
| 2019 | 2019-10-08 | 78435.0 | Makers Mark | Hy-Vee Food Store / Urbandale |
| 2021 | 2021-07-05 | 250932.0 | Titos Handmade Vodka | Hy-Vee #3 / BDI / Des Moines |
+------+------------+--------------+----------------------+-------------------------------+
or, you can get same result with a window function或者,您可以使用 window function 获得相同的结果
SELECT date, sale_dollars, item_description, store_name
FROM `bigquery-public-data.iowa_liquor_sales.sales`
WHERE date BETWEEN '2019-01-01' AND '2021-12-31'
QUALIFY ROW_NUMBER() OVER (
PARTITION BY EXTRACT(YEAR FROM date) ORDER BY sale_dollars DESC
) = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.