简体   繁体   中英

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'

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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