Oracle
 sql >> база данни >  >> RDS >> Oracle

Oracle SQL Select Matching Query

Можете да опитате този подход:

-- sample of data from the question
SQL> with t1(uid1, rid, time_type, date_time) as
  2  (
  3    select 'a11',  1, 1, '5/4/2013 00:32:00' from dual union all
  4    select 'a43',  2, 1, '5/4/2013 00:32:01' from dual union all
  5    select 'a68',  2, 2, '5/4/2013 00:32:02'  from dual union all
  6    select 'a98',  2, 1, '5/4/2013 00:32:03'  from dual union all
  7    select 'a45',  2, 1, '5/4/2013 00:32:04'  from dual union all
  8    select 'a94',  1, 2, '5/4/2013 00:32:05'  from dual union all
  9    select 'a35',  2, 2, '5/4/2013 00:32:07'  from dual union all
 10    select 'a33',  2, 2, '5/4/2013 00:32:08'  from dual
 11  ) -- the query
 12  select uid1
 13       , rid
 14       , time_type
 15       , date_time
 16    from (select uid1
 17               , rid
 18               , time_type
 19               , date_time
 20               , row_number() over(partition by rid, time_type order by rid) as rn
 21            from t1
 22          )
 23  order by rid, rn, time_type
 24  /

Резултат:

UID1        RID  TIME_TYPE DATE_TIME
---- ---------- ---------- -----------------
a11           1          1 5/4/2013 00:32:00
a94           1          2 5/4/2013 00:32:05
a43           2          1 5/4/2013 00:32:01
a68           2          2 5/4/2013 00:32:02
a98           2          1 5/4/2013 00:32:03
a35           2          2 5/4/2013 00:32:07
a45           2          1 5/4/2013 00:32:04
a33           2          2 5/4/2013 00:32:08

8 rows selected

Демо на SQLFiddle




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Страница, показваща „?“, вместо „é“

  2. Компилаторът pro*C на Oracle и gnu C (__builtin_va_list, __attribute__ и т.н.)

  3. oci8, php7 и Oracle 10.1 съвместимост

  4. Oracle DB:Как мога да напиша заявка без главни и главни букви?

  5. Грешка на Oracle ORA-06512