2

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?

1
  • Your table mytemp still has two columns, price1 and price2. And you want to produce rows where one of the prices is filled and the other is empty? So if a mytable row'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. Commented Mar 7, 2019 at 11:42

4 Answers 4

3

you can use case when

  SELECT
   sum(case when timestamp1 > NOW() - INTERVAL 3 HOUR then price1 else 0 end) as price1,
    sum(case when timestamp2 > NOW() - INTERVAL 3 HOUR then price2 else 0 end)
    as price2
     FROM mytable  ;

but if you want insert just select

INSERT INTO mytemp (price1,price2)
SELECT
   sum(case when timestamp1 > NOW() - INTERVAL 3 HOUR then price1 else 0 end) as price1,
    sum(case when timestamp2 > NOW() - INTERVAL 3 HOUR then price2 else 0 end)
    as price2
     FROM mytable
Sign up to request clarification or add additional context in comments.

1 Comment

Correct me If I'm wrong. This query will result in insertion of one row but that is not what OP's current queries are doing right?
1

try this using case when

INSERT INTO mytemp (price1,price2)
SELECT max(case when timestamp1 > NOW() - INTERVAL 3 HOUR then price1 end),max(case when timestamp2 > NOW() - INTERVAL 3 HOUR then price2 end) 
FROM mytable 

Comments

0

You can use cross join to unpivot the data:

select (case when n.n = 1 then price1
             when n.n = 2 then price2
        end),
       (case when n.n = 1 then timestamp1
             when n.n = 2 then timestamp2
        end)       
from t cross join
     (select 1 as n union all select 2) n
having timestamp > now() - interval 3 hour;

However, your method or the equivalent with union all is likely to be the fastest method if you have an index on timestamp.

Comments

0

try this

SELECT price1 , '' as price2 from testa WHERE timestamp1 > NOW() - INTERVAL 3 HOUR
UNION
SELECT '' as price1, price2  as price from testa WHERE timestamp2 > NOW() - INTERVAL 3 HOUR

2 Comments

price2 is missing in the results for this.
Don't use double quotes for string literals. Use single quotes as defined in the SQL standard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.