sql >> Databáze >  >> RDS >> Oracle

SQL MIN hodnoty ze dvou tabulek

Nejbližší, co mohu získat, je toto:Všimnete si, že můj níže uvedený dotaz ukazuje některé začátky bez konců .. nevysvětlíte, co s nimi dělat. Můj dotaz také spojuje různé začátky a konce od vás. Řekl jsi

Což jsem udělal níže a odkazuje to:"264 s 4280", ne "165 s 4280", jak jste ukázali. Vzhledem k tomu, že 262 bylo spárováno s 290, znamená to, že 264 je nyní MIN, které má být porovnáno s MIN úspěšnosti, 4280. Pokud můžete dále upřesnit své požadavky, mohu dotaz upravit.

  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. Dotaz na výběr více dat spolu s průměrnými hodnotami v určitém čase pomocí Postgres

  2. MySQL - výběr v blízkosti prostorového bodu

  3. Použijte PHP k výpisu hodnot mysql do souboru JSON

  4. Vysvětlení rámce MySQL High Availability Framework – Část III:Scénáře selhání