I have a table:
DATE WEATHERSTATION TEMPERATURE RAINFALL
1975-01-10 B81A 25 0
1975-01-11 K174 33 16
1975-01-12 ZG4N 18 3
I build this table by first processing an input that contains the date and temperature, and then an input that contains the date and rainfall. Sometimes, there are dates in the rainfall data that don't exist in the temperature data (for example, if a weather station started recording rainfall in 1950 but temperatures in 1970 -- it happens).
What I'd like to do is say:
- If there's already a row with this date, update value X (which can be temperature or rainfall depending on the program);
- If there isn't a row with this date, create it.
Obviously, I can do this within the program itself, but I think doing it in the SQL would be more efficient and sensible.
If I use INSERT OR REPLACE INTO weatherdata (date, weatherstation, temperature)
, because that deletes the rainfall value. What I'd like is INSERT OR UPDATE INTO WEATHERDATA
, but that doesn't appear to exist; I've Googled it, and read StackOverflow posts about it, but nothing has really helped me.
What would be the proper thing to do in this situation?
(The database system is SQLite, if that matters.)