I'm having a table that has multiple price-timestamp tuples:
#mytable;
id;<somecolumns>;price1;timestamp1;price2;timestamp2;
I want to export each price-timestamp tuple in a temp table, but only the prices whose timestamp is within a specific time interval:
#mytemp
id;price1;price2;
I could achieve this by repeating the sql for each tuple:
INSERT INTO mytemp (price1)
SELECT price1 FROM mytable WHERE timestamp1 > NOW() - INTERVAL 3 HOUR;
INSERT INTO mytemp (price2)
SELECT price2 FROM mytable WHERE timestamp2 > NOW() - INTERVAL 3 HOUR;
#repeat for all price-timestamp tuples
Question: could I optimize this into one single sql query?
mytempstill has two columns,price1andprice2. And you want to produce rows where one of the prices is filled and the other is empty? So if amytablerow's timestamps both match, you want to split this in two rows? (At least this is what your inserts do.) And what does "#repeat for all price-timestamp tuples" mean? It may help a lot, if you show sample data and expected result.