简体   繁体   English

从列生成 MIN、AVG、MAX 列。 [SQL] 蜂巢

[英]Generating MIN, AVG, MAX columns from a column. [SQL] Hive

I have a database with a column for oil prices.我有一个包含油价列的数据库。 My goal is to generate a table with columns showing Min Price, AvgPrice, and Max Price based off of the Oil value column grouped by location.我的目标是根据按位置分组的石油价值列生成一个表格,其中包含显示最低价格、平均价格和最高价格的列。 Where oil.frequency = 'A'其中 oil.frequency = 'A'

this is my current query这是我当前的查询

select location, 
       min(oil.value) over (partition BY oil.location), 
       max(oil.value) over (partition BY oil.location), 
       avg(oil.value) over (partition BY oil.location)
FROM OIL 
Where oil.frequency = 'A'
GROUP BY oil.location, oil.value;

The images below show my data table and the 2nd image shows how I want the data to appear.下图显示了我的数据表,第二张图显示了我希望数据如何显示。

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

You don't need window functions.你不需要窗口函数。
Just group by location and aggregate:只需按位置分组并汇总:

select location, 
       min(value), 
       max(value), 
       avg(value)
FROM OIL 
Where frequency = 'A'
GROUP BY location;

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

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