简体   繁体   English

(SQL)您如何 select 一个最大浮点值以及查询中的其他数据类型值?

[英](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;
Query results查询结果
+------+------------+--------------+----------------------+-------------------------------+
| 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.

相关问题 当我的结果可能是 FLOAT 而不是 INTEGER 时,如何在计算一组值的平均值时将 FLOAT 合并到我的查询中以避免错误 - How do I incorporate FLOAT in my query when calculating the average of a set of values when my result could be a FLOAT and not INTEGER to avoid error Big Query SQL:如何聚合字段中包含的数组的总和? - Big Query SQL: How do you aggregate the sum of an array contained in a field? 您如何编组 sql.NullString 以使 output 展平以仅给出 go 中的值? - How do you marshal a sql.NullString such that the output is flattened to give just the value in go? 如果我使用 Firebase/Firestore 查询返回值,如何返回 TableView 数据源值? - How do I return TableView datasource values if I'm using Firebase/Firestore to query the returning value? Data Studio - 您可以在图表中显示最大指标的日期吗? - Data Studio - Can you display the date of a Max Metric within a Chart? 如何在大查询中根据时间戳最小最大值查询按某些类别分组的两个值的减法 - How can I query subtraction of two values grouped by certain categories based on timestamp min max on big query 如何将具有不同值的虚拟列添加到 select 查询的结果中? - How to add a dummy column with different values to the result of a select query? 如何获取记录中列值的最大值? (大查询) - How to get max value of column values in a record ? (BigQuery) 如何提取 0 后的字符串:在大查询 sql - How do I extract the string after 0: in big query sql 使用 Big Query SQL 我可以实现以下减法(减去两行值并将其替换为该值)? - Using Big Query SQL I can I achieve the following subtraction ( subtract two row values and replace it with that value )?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM