Skip to content
Advertisement

Displaying Unique Temperature Values for the US

I am looking to do something similar to what I did in R (below) in SQL:

The output of this code looks like this:

So far in SQL I have managed to output a single state using the following code:

The output looks like this:

However, I am unable to get the unique avgTemp for each state. When I take it to more than one state, I get the same avgTemp for all states in that query. So if I run a query like this:

I get the average for those states.

I am assuming I need to write a subquery of some sort to iterate through each state and give the avgTemp. I tried to partition over the state as well and it didn’t give me what I wanted. My overall goal is to print out each states avgTemp for the given year.

The dataset contains multiple values for each year and state:

With more Alabama entries and the other ‘A’ states below it, so I think using distinct year is imperative. I am just stuck and expanding the single state out into multiple without then taking the average of all the states in the query. The data set can be found here: https://www.kaggle.com/berkeleyearth/climate-change-earth-surface-temperature-data?select=GlobalLandTemperaturesByState.csv

Thanks for the help!

Advertisement

Answer

Since you are looking for the average temperature for each state, you should add the State column to the partition by clause.

Try:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement