Гость
Форумы / Oracle [игнор отключен] [закрыт для гостей] / запросик / 7 сообщений из 7, страница 1 из 1
21.05.2021, 11:42
    #40071891
Anton_Demin
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
запросик
добрый день

Есть запрос который возвращает набор документов с атрибутами.
Если во всей выборке присутствуют документы с типом F , то отображаем только их. Если документов с типом F нет, то отображаем всю выборку

ниже, мой вариант, подкиньте ещё решения, а то мне не нравится реализация, а в голову ничего не приходит
Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
with docs as (        
        select 1 id, 'doc1' name, 'E' type from dual union all
        select 2 id, 'doc2' name, 'E' type from dual union all
        select 3 id, 'doc3' name, 'E' type from dual union all
        select 4 id, 'doc4' name, 'F' type from dual union all
        select 5 id, 'doc5' name, 'F' type from dual union all
        select 6 id, 'doc6' name, 'F' type from dual union all
        select 7 id, 'doc7' name, 'G' type from dual union all
        select 8 id, 'doc8' name, 'G' type from dual)
select id, name, type         
  from (select id, 
               name, 
               type,
               sum(case when type = 'F' then 1 else 0 end) over () cnt_type_f
          from docs)
  where (cnt_type_f = 0 or (cnt_type_f <> 0 and type = 'F'))
...
Рейтинг: 0 / 0
21.05.2021, 11:54
    #40071897
AmKad
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
запросик
Anton_Demin,

Вариант рабочий, но при подсчете количества для явного выражения намерений я предпочитаю использовать функцию count.
...
Рейтинг: 0 / 0
21.05.2021, 12:16
    #40071904
xyluGUN
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
запросик
Можно так:
Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
with docs as (        
        select 1 id, 'doc1' name, 'E' type from dual union all
        select 2 id, 'doc2' name, 'E' type from dual union all
        select 3 id, 'doc3' name, 'E' type from dual union all
        select 4 id, 'doc4' name, 'F' type from dual union all
        select 5 id, 'doc5' name, 'F' type from dual union all
        select 6 id, 'doc6' name, 'F' type from dual union all
        select 7 id, 'doc7' name, 'G' type from dual union all
        select 8 id, 'doc8' name, 'G' type from dual)
select id, name, type         
  from docs
 where not exists (select 1
                     from docs f
                    where f.type = 'F')
union all
select id, name, type         
  from docs
 where type = 'F'
...
Рейтинг: 0 / 0
21.05.2021, 12:31
    #40071910
oragraf
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
запросик
xyluGUN
Можно так:[/src]
Нужно еще больше чтений.
...
Рейтинг: 0 / 0
21.05.2021, 12:58
    #40071919
andrey_anonymous
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
запросик
Код: 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.
with docs as (        
...)
select coalesce(d_f.id, d_x.id) id
     , coalesce(d_f.name, d_x.name) name
     , coalesce(d_f.type, d_x.type) type
  from dual
  left join docs d_f on (d_f."TYPE" = 'F')
  left join docs d_x on (d_f."TYPE" is null)
;
--------------------------
with docs as (        
...)
, d as (select d_f.id fId, d_x.id xId
     , d_f.name fName, d_x.name xName
     , d_f.type fType, d_x.type xType
  from dual
  left join docs d_f on (d_f."TYPE" = 'F')
  left join docs d_x on (d_f."TYPE" is null)
) select id, name, type
    from d
  unpivot((id, name, type) for c in ((fid, fname, ftype) as 'f',(xid, xname, xtype) as 'x')
)  
;
--------------------------
with docs as (        
...)
, d as (select d.*, max(case "TYPE" when 'F' then 'F' end) over() ff
          from docs d)
select * 
  from d
 where nvl(ff,"TYPE") = "TYPE"
;
--------------------------
with docs as (        
...)
, d as (select docs.*, case "TYPE" when 'F' then 0 else 1 end ord from docs)        
select * from d
match_recognize(
order by ord, id
measures match_number() as mno
all rows per match
pattern( (f_entity+ | non_f_entity+ ))
define f_entity as "TYPE" = 'F'
     , non_f_entity as lnnvl("TYPE" = 'F')
)
where mno = 1
;
--------------------------
-- вообще дофига можно придумать
...
Рейтинг: 0 / 0
21.05.2021, 20:15
    #40072046
Леонид33
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
запросик
если я правильно понял, что надо, то это, наверное, самое простое:
select * from docs where
type=case when 'F'=(select distinct type FROM docs where type='F') then 'F' else type end
...
Рейтинг: 0 / 0
22.05.2021, 01:03
    #40072112
SY
SY
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
запросик
andrey_anonymous

-- вообще дофига можно придумать


Ну match_recognize можно и упростить:

Код: 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.
SQL> with docs as (
  2                select 1 id, 'doc1' name, 'E' type from dual union all
  3                select 2 id, 'doc2' name, 'E' type from dual union all
  4                select 3 id, 'doc3' name, 'E' type from dual union all
  5                select 4 id, 'doc4' name, 'F' type from dual union all
  6                select 5 id, 'doc5' name, 'F' type from dual union all
  7                select 6 id, 'doc6' name, 'F' type from dual union all
  8                select 7 id, 'doc7' name, 'G' type from dual union all
  9                select 8 id, 'doc8' name, 'G' type from dual
 10               ),
 11          d as (select docs.*, case "TYPE" when 'F' then 0 else 1 end ord from docs)
 12  select  *
 13    from  d
 14    match_recognize(
 15                    order by ord,
 16                             id
 17                    all rows per match
 18                    pattern(^p+)
 19                    define p as ord = first(ord)
 20                   )
 21  /

       ORD  ID NAME        T
---------- --- ----------- -
         0   4 doc4        F
         0   5 doc5        F
         0   6 doc6        F

SQL> with docs as (
  2                select 1 id, 'doc1' name, 'E' type from dual union all
  3                select 2 id, 'doc2' name, 'E' type from dual union all
  4                select 3 id, 'doc3' name, 'E' type from dual union all
  5                select 4 id, 'doc4' name, 'X' type from dual union all
  6                select 5 id, 'doc5' name, 'X' type from dual union all
  7                select 6 id, 'doc6' name, 'X' type from dual union all
  8                select 7 id, 'doc7' name, 'G' type from dual union all
  9                select 8 id, 'doc8' name, 'G' type from dual
 10               ),
 11          d as (select docs.*, case "TYPE" when 'F' then 0 else 1 end ord from docs)
 12  select  *
 13    from  d
 14    match_recognize(
 15                    order by ord,
 16                             id
 17                    all rows per match
 18                    pattern(^p+)
 19                    define p as ord = first(ord)
 20                   )
 21  /

       ORD  ID NAME        T
---------- --- ----------- -
         1   1 doc1        E
         1   2 doc2        E
         1   3 doc3        E
         1   4 doc4        X
         1   5 doc5        X
         1   6 doc6        X
         1   7 doc7        G
         1   8 doc8        G

8 rows selected.

SQL>



SY.
...
Рейтинг: 0 / 0
Форумы / Oracle [игнор отключен] [закрыт для гостей] / запросик / 7 сообщений из 7, страница 1 из 1
Целевая тема:
Создать новую тему:
Автор:
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


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