M
M
moem2018-08-10 11:50:44
SQL
moem, 2018-08-10 11:50:44

How to extract dataset from two tables with different time parameters?

Hello.
Prompt or suggest an idea, please, who knows.
Given :
table 1 of the form:

datetimeparam1
2018-08-01 12:10red
2018-08-02 15:20green
2018-08-03 08:30blue
table 2 types:
datetimeparam2
2018-08-01 12:15123
2018-08-01 13:20234
2018-08-01 22:30345
2018-08-02 16:10456
2018-08-02 21:15567
The value of parameter 1 changes once a day. The value of parameter 2 changes thousands of times a day.
Required to get :
datetimeparam2param1
2018-08-01 12:15123red
2018-08-01 13:20234red
2018-08-01 22:30345red
2018-08-02 16:10456green
2018-08-02 21:15567green
Those. a data set with the values ​​of date-time and parameter 2 from table 2 and the current value of parameter 1 from table 1.
Is it possible to get this with one fast enough sql query?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ponaehal, 2018-08-30
@ponaehal

Hmm... For oracle, I would get out like this:

SELECT  
t2.datetime
, param2
, (SELECT param1 
  FROM
     (SELECT 
         t1.param1 
      ,  row_number() OVER(ORDER BY t1.datetime DESC)  rankparam1
      FROM table1 t1  
      WHERE t2.datetime>=t1.datetime)  
   WHERE rankparam1 = 1)
  as param1
FROM table2  t2

I believe in others the base is about the same.
But I'm not sure that it will be fast enough (even if you place the indexes correctly).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question