Select statement to compare values within same field

1382
8
11-04-2016 05:50 PM
DCWORK_GIS
New Contributor III

I need help creating a select statement for an attribute table of temperature data. The table has fields for Station_Name, Date and Max_Temp and I would like to select only those records where the temperature difference between weather stations was 20 degrees or more in the same day. I'm stumped on how to compare the 18 Max_Temp values in any single day for each of 365 Dates.

0 Kudos
8 Replies
JayantaPoddar
MVP Esteemed Contributor

Using Summary Statistics—Help | ArcGIS for Desktop , you can get the Min. and Max. temperature of each date. Then, in the new table, you could find the temp. difference using field calculator (or using MS Excel).



Think Location
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Your options in situations like this depend on several factors.  How are you storing the information currently?  If it is stored in shapefiles or a file geodatabase, can it be stored in other data stores that support more robust SQL?  How do you want to work with it afterwards?  Is this a once-and-done kind of action or something you will be repeating over and over?

DCWORK_GIS
New Contributor III

Thanks Joshua. The data comes from a Hosted Feature Class on arcgis.com. I'm going to try exporting it into a MySql DB and working with the data there. I'm not sure what steps to take to get the outliers - I'm thinking it can be done with subqueries, which I've never written, but I think I need to learn 🙂

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

After reading over your question a couple of times, I must admit I am not completely clear what you are trying to select.  Since scaling from 1 day to 365 days will be straightforward, let's talk about a single day.  There is what I believe you have for information:

  • 18 records for a given date, one record for each of 18 weather stations
  • For each weather station and date, a single maximum temperature is recorded

I get a bit confused with, "select only those records where the temperature difference between weather stations was 20 degrees or more in the same day."  Are you wanting to compare each weather station to the other 17?  If so, do you select a pair of weather station records if the maximum temperature is 20 degree apart?  If that is the case, won't the output be pairs of stations by date?

If you could mock up what you expect the results to look like, it would be helpful.

0 Kudos
DCWORK_GIS
New Contributor III

I ended up using your suggestion and pulled it into a MySql DB and queried it there, then exported the tables as CSV and imported those into the GDB. I used two subqueries to test for the upper and lower deviations from the average like so:

SELECT `ObjectID`, `Date`, `StationName`, `MaxTemp`  FROM  `DailyTemps` WHERE    `MaxTemp` > (SELECT AVG(`MaxTemp`) FROM `DailyTemps`) +10OR    `MaxTemp` < (SELECT AVG(`MaxTemp`) FROM `DailyTemps`) -10

I ultimately exported two tables - 1 for extreme highs and one for extreme lows. I'll display XY locations for each table using graduated color symbology to illustrate the extremes.

I should have added that this is all for time-aware layers so the map shows the temperature changes over time.

0 Kudos
DCWORK_GIS
New Contributor III

Shame we can't add formatted code into these messages - here's a screenshot:

Select Statement with 2 subqueries

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I don't believe the query you provided is giving the answer you expect.  As written, you are using the averages over the entire data set, not just one day, which is what I thought you were wanting.  Try the following, let me know if it works for you (may not be formatted completely correctly for MySQL😞

SELECT  dt.ObjectID ,
        dt.Date ,
        dt.StationName ,
        dt.MaxTemp ,
        at.AvgMax
FROM    DailyTemps dt
        INNER JOIN ( SELECT Date ,
                            AVG(MaxTemp) AS AvgMax
                     FROM   DailyTemps
                     GROUP BY Date
                   ) AS at ON dt.Date = at.Date
WHERE   ABS(dt.MaxTemp - at.AvgMax) > 10