Oracle
 sql >> Baza danych >  >> RDS >> Oracle

Wartości SQL MIN z dwóch tabel

Najbliższe, co mogę uzyskać, to:Zauważysz, że moje poniższe zapytanie pokazuje niektóre początki bez końca .. nie wyjaśniasz, co z nimi zrobić. Ponadto moje zapytanie łączy różne początki i zakończenia od Ciebie. Powiedziałeś

Co zrobiłem poniżej, a łączy ono:„264 z 4280”, a nie „165 z 4280”, jak pokazałeś. Ponieważ 262 zostało sparowane z 290, oznacza to, że 264 jest teraz MIN, który ma być dopasowany do MIN sukcesu, czyli 4280. Jeśli możesz dokładniej wyjaśnić swoje wymagania, mogę dostosować zapytanie.

  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.


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. OracleConnection zgłasza pusty wyjątek

  2. Zapytanie o relacje klucza obcego tabeli

  3. Jak zdobyć pierwszy i ostatni dzień tygodnia w Oracle?

  4. Rozwiązanie Gaps and Islands w Oracle - wykorzystanie rekurencji

  5. SQL - Jak wybrać wiersz mający kolumnę z maksymalną wartością (+ grupuj według)