Oracle
 sql >> Database >  >> RDS >> Oracle

Valori SQL MIN da due tabelle

Il più vicino che posso ottenere è questo:noterai che la mia query di seguito mostra alcuni inizi senza fine .. non spieghi cosa fare con quelli. Inoltre, la mia query collega diversi inizi e finali da te. Hai detto

Che ho fatto di seguito, e collega:"264 con 4280", non "165 con 4280" come hai mostrato. Poiché 262 è stato accoppiato con 290, ciò significa che 264 è ora il MIN da abbinare al MIN di successo, 4280. Se puoi chiarire ulteriormente i tuoi requisiti, posso modificare la query.

  with w_start as (
           select 1234 id1, to_date('05/14/2014 10:02:29','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 262 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 10:02:31','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 264 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 10:02:45','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 278 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 10:02:50','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 285 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 10:09:04','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 165 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 10:09:06','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 2167') msg from dual union all
           select 1234 id1, to_date('05/14/2014 10:09:16','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 2180') msg from dual union all
           select 1234 id1, to_date('05/14/2014 10:09:26','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 2190') msg from dual union all
           select 1234 id1, to_date('05/14/2014 11:45:11','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 8767') msg from dual union all
           select 1234 id1, to_date('05/14/2014 16:48:20','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 878 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 19:02:52','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abc' jobid, rtrim('start 687 ') msg from dual union all
           select 5678 id1, to_date('05/14/2014 22:02:52','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'pqr' jobid, rtrim('start 501 ') msg from dual union all
           select 5678 id1, to_date('05/14/2014 23:10:40','mm/dd/yyyy hh24:mi:ss') t_start, to_date('05/14/2014','mm-dd-yyyy') dt, 'abcd' jobid, rtrim('start 200 ') msg from dual 
           ),
     w_success as (
           select 1234 id1, to_date('05/14/2014 10:02:52', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/14/2014', 'mm/dd/yyyy') dt, 'abc' jobid, rtrim('successful 290  ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 10:09:32', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/14/2014', 'mm/dd/yyyy') dt, 'abc' jobid, rtrim('successful 4280 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 11:45:15', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/14/2014', 'mm/dd/yyyy') dt, 'abc' jobid, rtrim('successful 8774 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 11:45:18', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/14/2014', 'mm/dd/yyyy') dt, 'abc' jobid, rtrim('successful 8777 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 11:45:19', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/14/2014', 'mm/dd/yyyy') dt, 'abc' jobid, rtrim('successful 8778 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 11:45:25', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/14/2014', 'mm/dd/yyyy') dt, 'abc' jobid, rtrim('successful 8784 ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 16:48:22', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/14/2014', 'mm/dd/yyyy') dt, 'abc' jobid, rtrim('successful 880  ') msg from dual union all
           select 1234 id1, to_date('05/14/2014 19:03:00', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/14/2014', 'mm/dd/yyyy') dt, 'abc' jobid, rtrim('successful 699  ') msg from dual union all
           select 5678 id1, to_date('05/14/2014 22:03:00', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/14/2014', 'mm/dd/yyyy') dt, 'pqr' jobid, rtrim('successful 250  ') msg from dual union all
           select 5678 id1, to_date('05/19/2014 14:00:16', 'mm/dd/yyyy hh24:mi:ss') t_end, to_date('05/19/2014', 'mm/dd/yyyy') dt, 'pqr' jobid, rtrim('successful 400  ') msg from dual 
           ),
     w_sub1 as (
           select id1, jobid, dt, t_start, msg,
                    row_number() over (partition by id1, jobid
                                      order by t_start ) l1
             from w_start
           ),
     w_sub2 as (
           select id1, jobid, dt, t_end, msg,
                    row_number() over (partition by id1, jobid
                                      order by t_end ) l2
             from w_success
           )
  select nvl(a.id1,b.id1) id1, nvl(a.jobid, b.jobid) jobid, nvl(a.dt, b.dt) dt, 
           t_start, t_end, 
           a.msg, b.msg, l1, l2
    from w_sub1  a
        FULL OUTER JOIN w_sub2  b
           ON a.id1      = b.id1
          and a.jobid    = b.jobid
          and a.dt       = b.dt
          and a.l1       = b.l2
   order by 1, 4, 5, 2
  /

         ID1 JOBI DT                   T_START              T_END                MSG        MSG                     L1         L2
  ---------- ---- -------------------- -------------------- -------------------- ---------- --------------- ---------- ----------
        1234 abc  14-may-2014 00:00:00 14-may-2014 10:02:29 14-may-2014 10:02:52 start 262  successful 290           1          1
        1234 abc  14-may-2014 00:00:00 14-may-2014 10:02:31 14-may-2014 10:09:32 start 264  successful 4280          2          2
        1234 abc  14-may-2014 00:00:00 14-may-2014 10:02:45 14-may-2014 11:45:15 start 278  successful 8774          3          3
        1234 abc  14-may-2014 00:00:00 14-may-2014 10:02:50 14-may-2014 11:45:18 start 285  successful 8777          4          4
        1234 abc  14-may-2014 00:00:00 14-may-2014 10:09:04 14-may-2014 11:45:19 start 165  successful 8778          5          5
        1234 abc  14-may-2014 00:00:00 14-may-2014 10:09:06 14-may-2014 11:45:25 start 2167 successful 8784          6          6
        1234 abc  14-may-2014 00:00:00 14-may-2014 10:09:16 14-may-2014 16:48:22 start 2180 successful 880           7          7
        1234 abc  14-may-2014 00:00:00 14-may-2014 10:09:26 14-may-2014 19:03:00 start 2190 successful 699           8          8
        1234 abc  14-may-2014 00:00:00 14-may-2014 11:45:11                      start 8767                          9
        1234 abc  14-may-2014 00:00:00 14-may-2014 16:48:20                      start 878                          10
        1234 abc  14-may-2014 00:00:00 14-may-2014 19:02:52                      start 687                          11
        5678 pqr  14-may-2014 00:00:00 14-may-2014 22:02:52 14-may-2014 22:03:00 start 501  successful 250           1          1
        5678 abcd 14-may-2014 00:00:00 14-may-2014 23:10:40                      start 200                           1
        5678 pqr  19-may-2014 00:00:00                      19-may-2014 14:00:16            successful 400                      2

  14 rows selected.