Как получить набор записей из таблицы с учетом их начальной и конечной точек в sql? - PullRequest
0 голосов
/ 03 апреля 2020

У меня есть 2 структуры таблиц:

TAB_1 ( TRN_ID , STN_CODE,DIST_FRM_SRC)    

TAB_2 ( TRN_ID , SRC_STN , DSTN_STN ) 

Данные в TAB_1:

enter image description here

Данные в TAB_2

enter image description here

Мне нужно извлечь все строки из TAB_1, соответствующего SRC_STN и DST_STN в TAB_2, поэтому OP будет

enter image description here

Пожалуйста, руководство.

Ответы [ 4 ]

1 голос
/ 03 апреля 2020

Вам необходимо рассмотреть целочисленную часть из кодов станций, которые имеют тип данных VARCHAR2 , а затем получить данные между целыми числами.

Поскольку коды станций имеет префикс S, я использовал LTRIM , чтобы обрезать его и преобразовать строку в число, используя TO_NUMBER :

SELECT
    t1.trn_id, t1.stn_code, t1.dist_frm_src
FROM
    tab_1   t1
    JOIN tab_2   t2 
    ON t1.trn_id = t2.trn_id
WHERE
    to_number(ltrim(t1.stn_code, 'S'))
    BETWEEN to_number(ltrim(t1.src_stn, 'S')) 
    AND     to_number(ltrim(t1.dstn_stn, 'S'))
ORDER BY t1.trn_id, t1.stn_code;
1 голос
/ 03 апреля 2020

Выглядит как простое соединение:

SQL> with
  2  tab_1 (trn_id, stn_code, dist_frm_src) as
  3    (select 100, 'GHU', 0 from dual union all
  4     select 100, 'SDP', 2 from dual union all
  5     select 100, 'DRK', 5 from dual union all
  6     select 100, 'SAB', 7 from dual union all
  7     select 100, 'DRT', 8 from dual union all
  8     select 100, 'POL', 10 from dual union all
  9     select 100, 'WRT', 15 from dual),
 10  tab_2 (trn_id, src_stn, dstn_stn) as
 11    (select 100, 'SDP', 'POL' from dual),
 12  --
 13  dfs as
 14    (select x.trn_id,
 15            a.dist_frm_src val_1,
 16            b.dist_frm_src val_2
 17     from tab_2 x join tab_1 a on x.trn_id = a.trn_id and x.src_stn = a.stn_code
 18                  join tab_1 b on x.trn_id = b.trn_id and x.dstn_stn = b.stn_code
 19    )
 20  select a.trn_id, a.stn_code, a.dist_frm_src
 21  from tab_1 a join dfs d on a.trn_id = d.trn_id
 22                         and a.dist_frm_src between d.val_1 and d.val_2
 23  order by trn_id, a.dist_frm_src;

    TRN_ID STN DIST_FRM_SRC
---------- --- ------------
       100 SDP            2
       100 DRK            5
       100 SAB            7
       100 DRT            8
       100 POL           10

SQL>
0 голосов
/ 03 апреля 2020

Используйте простое объединение с оконными функциями:

select t12.TRN_ID, t12.STN_CODE, t12.DIST_FRM_SRC
from (select t2.*,
             min(case when t1.stn_code = t2.src_stn then dist_frm_src end) as first_dist_frm_src,
             max(case when t1.stn_code = t2.dst_stn then dist_frm_src end) as last_dist_frm_src
      from tab_1 t1 join
           tab_2 t2
           on t1.trn_id = t2.trn_id
     ) t12
where t12.dist_fm_src between first_dist_frm_src and last_dist_frm_src;
0 голосов
/ 03 апреля 2020

Вы можете join:

select t1.*
from tab_1 t1
inner join tab_2 t2 
    on t1.trnid = t2.trnid and t1.stn_code between t2.src_stn and t2.dstn_stn

Другой вариант использует not exists; это удобно, если в tab_2 могут быть перекрывающиеся интервалы:

select t1.*
from tab_1 t1
where exists (
    select 1 
    from tab_2 t2 
    where t1.trnid = t2.trnid and t1.stn_code between t2.src_stn and t2.dstn_stn
)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...