powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Oracle [игнор отключен] [закрыт для гостей] / Выбор промежуточных значений запросом
7 сообщений из 32, страница 2 из 2
Выбор промежуточных значений запросом
    #39371272
Фотография SY
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
leprechaun,

Ты прaвила уточни. Есть :P_ORG_FROM и :P_ORG_TO:

1. Существуют organization_id с :P_ORG_FROM и :P_ORG_TO - Вывести все organization_id между :P_ORG_FROM и :P_ORG_TO из строк отсортированных по set_of_books_id,short_code,organization_id.

2. Существуeт только organization_id с :P_ORG_FROM (это включает в себя :P_ORG_TO IS NULL) - Вывести все organization_id начиная с :P_ORG_FROM и до конца из строк отсортированных по set_of_books_id,short_code,organization_id.

3. Существуeт только organization_id с :P_ORG_TO (это включает в себя :P_ORG_FROM IS NULL) - Вывести все organization_id между начиная с первой до (включительно) :P_ORG_TO из строк отсортированных по set_of_books_id,short_code,organization_id.

4. Не существуют organization_id с :P_ORG_FROM и :P_ORG_TO (это включает в себя :P_ORG_FROM IS NULL и :P_ORG_TO IS NULL) - Вывести все organization_id отсортированныe по set_of_books_id,short_code,organization_id.

Если да, то:

Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
      and t1.org_id IN (
                        with x as (
                                   select  t.*,
                                           count(case organization_id when :P_ORG_FROM then 1 end) over() lower_bound_flag
                                     from  hr_operating_units t
                                  )
                        select  organization_id
                          from  t
                          match_recognize(
                                          order by set_of_books_id,short_code,organization_id
                                          all rows per match
                                          pattern(up+)
                                          define
                                            up as     match_number() = 1
                                                  and (
                                                          first(organization_id) = :P_ORG_FROM
                                                       or
                                                          lower_bound_flag = 0
                                                      )
                                                  and 
                                                      nvl(
                                                          prev(organization_id),
                                                          0
                                                         ) != nvl(
                                                                  :P_ORG_TO,
                                                                  -1
                                                                 )
                                         )
                       )



SY.
...
Рейтинг: 0 / 0
Выбор промежуточных значений запросом
    #39371284
Фотография SY
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Хотя тут напрашивается:

Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
      and (
              (
                   :P_ORG_FROM is null
               and
                   :P_ORG_TO is null
              )
           or t1.org_id IN (
                            with x as (
                                       select  t.*,
                                               count(case organization_id when :P_ORG_FROM then 1 end) over() lower_bound_flag
                                         from  hr_operating_units t
                                      )
                            select  organization_id
                              from  t
                              match_recognize(
                                              order by set_of_books_id,short_code,organization_id
                                              all rows per match
                                              pattern(up+)
                                              define
                                                up as     match_number() = 1
                                                      and (
                                                              first(organization_id) = :P_ORG_FROM
                                                           or
                                                              lower_bound_flag = 0
                                                          )
                                                      and 
                                                          nvl(
                                                              prev(organization_id),
                                                              0
                                                             ) != :P_ORG_TO
                                             )
                           )
          )



SY.
...
Рейтинг: 0 / 0
Выбор промежуточных значений запросом
    #39371391
leprechaun
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Не работает что-то..

еще раз, такой кусок работает без проблем, но если P_ORG_FROM - первая запись отсортированной таблицы, то данных нет

Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
     
     select organization_id from(
      select organization_id,
          name,
          short_code,
          set_of_books_id
      from  hr_operating_units
      match_recognize(
            order by set_of_books_id,short_code,organization_id
            all rows per match
            pattern(up+)
            define
            up as first(up.organization_id) = nvl(:P_ORG_FROM, (select organization_id from (select rownum str_number, organization_id from (select organization_id from hr_operating_units order by set_of_books_id, short_code)) where str_number = 2))
            and prev(up.organization_id) != nvl(:P_ORG_TO, 0))
            )



приведенный вами код пытаюсь запустить

Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
select organization_id from(
  (WITH x AS (SELECT t.*, COUNT(CASE organization_id WHEN :P_ORG_FROM THEN 1 END) over() lower_bound_flag FROM hr_operating_units t)
    SELECT organization_id FROM x  -- тут только поправил имя таблицы
    match_recognize(ORDER BY set_of_books_id,short_code,organization_id ALL rows per MATCH 
    pattern(up+)
    define 
    up AS match_number() = 1
    AND (FIRST(organization_id) = :P_ORG_FROM OR lower_bound_flag      = 0) AND NVL( prev(organization_id), 0 ) != NVL( :P_ORG_TO, -1 ))
    ))



ORA-00932: несовместимые типы данных: ожидается NUMBER, получено CHAR

Передаю разумеется число. Что еще нужно поправить? Спасибо!
...
Рейтинг: 0 / 0
Выбор промежуточных значений запросом
    #39371427
Фотография SY
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
leprechaunORA-00932: несовместимые типы данных: ожидается NUMBER, получено CHAR


Где? На какой строке? Покажи выолнение в SQL*Plus.

Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
SQL> select  *
  2    from  hr_operating_units
  3  /

ORGANIZATION_ID S SET_OF_BOOKS_ID
--------------- - ---------------
             10 A               1
             13 B               1
             21 B               1
             14 C               2
             66 D               2
             55 D               3
             24 F               3
             11 E               3

8 rows selected.

SQL> variable P_ORG_FROM number
SQL> variable P_ORG_TO number
SQL> with x as (
  2             select  t.*,
  3                     count(case organization_id when :P_ORG_FROM then 1 end) over() lower_bound_flag
  4               from  hr_operating_units t
  5            )
  6  select  organization_id
  7    from  x
  8    match_recognize(
  9                    order by set_of_books_id,short_code,organization_id
 10                    all rows per match
 11                    pattern(up+)
 12                    define
 13                      up as     match_number() = 1
 14                            and (
 15                                    first(organization_id) = :P_ORG_FROM
 16                                 or
 17                                    lower_bound_flag = 0
 18                                )
 19                            and 
 20                                nvl(
 21                                    prev(organization_id),
 22                                    0
 23                                   ) != nvl(
 24                                            :P_ORG_TO,
 25                                            -1
 26                                           )
 27                   )
 28  /

ORGANIZATION_ID
---------------
             10
             13
             21
             14
             66
             55
             11
             24

8 rows selected.

SQL> exec :P_ORG_FROM := 21

PL/SQL procedure successfully completed.

SQL> with x as (
  2             select  t.*,
  3                     count(case organization_id when :P_ORG_FROM then 1 end) over() lower_bound_flag
  4               from  hr_operating_units t
  5            )
  6  select  organization_id
  7    from  x
  8    match_recognize(
  9                    order by set_of_books_id,short_code,organization_id
 10                    all rows per match
 11                    pattern(up+)
 12                    define
 13                      up as     match_number() = 1
 14                            and (
 15                                    first(organization_id) = :P_ORG_FROM
 16                                 or
 17                                    lower_bound_flag = 0
 18                                )
 19                            and 
 20                                nvl(
 21                                    prev(organization_id),
 22                                    0
 23                                   ) != nvl(
 24                                            :P_ORG_TO,
 25                                            -1
 26                                           )
 27                   )
 28  /

ORGANIZATION_ID
---------------
             21
             14
             66
             55
             11
             24

6 rows selected.

SQL> exec :P_ORG_TO := 55;

PL/SQL procedure successfully completed.

SQL> with x as (
  2             select  t.*,
  3                     count(case organization_id when :P_ORG_FROM then 1 end) over() lower_bound_flag
  4               from  hr_operating_units t
  5            )
  6  select  organization_id
  7    from  x
  8    match_recognize(
  9                    order by set_of_books_id,short_code,organization_id
 10                    all rows per match
 11                    pattern(up+)
 12                    define
 13                      up as     match_number() = 1
 14                            and (
 15                                    first(organization_id) = :P_ORG_FROM
 16                                 or
 17                                    lower_bound_flag = 0
 18                                )
 19                            and 
 20                                nvl(
 21                                    prev(organization_id),
 22                                    0
 23                                   ) != nvl(
 24                                            :P_ORG_TO,
 25                                            -1
 26                                           )
 27                   )
 28  /

ORGANIZATION_ID
---------------
             21
             14
             66
             55

SQL> exec :P_ORG_FROM := null

PL/SQL procedure successfully completed.

SQL> with x as (
  2             select  t.*,
  3                     count(case organization_id when :P_ORG_FROM then 1 end) over() lower_bound_flag
  4               from  hr_operating_units t
  5            )
  6  select  organization_id
  7    from  x
  8    match_recognize(
  9                    order by set_of_books_id,short_code,organization_id
 10                    all rows per match
 11                    pattern(up+)
 12                    define
 13                      up as     match_number() = 1
 14                            and (
 15                                    first(organization_id) = :P_ORG_FROM
 16                                 or
 17                                    lower_bound_flag = 0
 18                                )
 19                            and 
 20                                nvl(
 21                                    prev(organization_id),
 22                                    0
 23                                   ) != nvl(
 24                                            :P_ORG_TO,
 25                                            -1
 26                                           )
 27                   )
 28  /

ORGANIZATION_ID
---------------
             10
             13
             21
             14
             66
             55

6 rows selected.

SQL> 



SY.
...
Рейтинг: 0 / 0
Выбор промежуточных значений запросом
    #39371815
leprechaun
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Хм.. стыдно сказать SQL*Plus не установлен, удаленно через последний SQL Developer делаю.
Может и в оболочке проблема.
Но вставил этот код как условие для внешнего запроса - выдало ORA-32034: не поддерживается использование фразы WITH
А в девелопере не выполняется временная таблица

select t.*, count(case organization_id when :P_ORG_FROM then 1 end) over() lower_bound_flag from hr_operating_units t

ORA-00932: несовместимые типы данных: ожидается NUMBER, получено CHAR
P_ORG_FROM передаю число.
...
Рейтинг: 0 / 0
Выбор промежуточных значений запросом
    #39371844
leprechaun
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Поставил пока костыль, т.к. время поджимает

Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
     select organization_id from(
        (select organization_id, name, short_code, set_of_books_id
         from hr_operating_units
         --временный костыль
         union all
         select * from
         (select organization_id, name, short_code, set_of_books_id
         from hr_operating_units
         order by set_of_books_id, short_code)
         where rownum =1)
      match_recognize(
            order by set_of_books_id,short_code,organization_id
            all rows per match
            pattern(up+)
            define
            up as first(up.organization_id) = nvl(:P_ORG_FROM, (select * from (select organization_id from hr_operating_units order by set_of_books_id, short_code) where rownum = 1))
            and prev(up.organization_id) != nvl(:P_ORG_TO, 0))
            )



В любом случае спасибо огромное за помощь!
...
Рейтинг: 0 / 0
Выбор промежуточных значений запросом
    #39371975
leprechaun
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
+ И да, еще эта конструкция выводит пустой список, если :P_ORG_TO - первое значение в отсортированной таблице. Далее нормально.
...
Рейтинг: 0 / 0
7 сообщений из 32, страница 2 из 2
Форумы / Oracle [игнор отключен] [закрыт для гостей] / Выбор промежуточных значений запросом
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]