Преобразование запроса в представление - PullRequest
1 голос
/ 22 октября 2019

У меня длинный запрос, который насчитывает несколько десятков, основываясь на периодах. С учетом сказанного, у меня есть период P1 (который переводится в 07:00:00 и 08:59:59, и так и происходит. Мне нужно вычислить все числа по медикам и периоду. Я использую связанное значение для поиска в таблице(trunc (a.dt_agenda, 'dd') между: dt_inicial и: dt_final) Поскольку я использую UNION ALL для объединения всех периодов, запрос довольно длинный, и мне нужно сохранить его в поле varchar2 (4000). Итак, мне нужно создать объект из моего запроса (возможно, представление), таким образом, что в запросе ищет представление вместо таблицы. Представления не принимают связанные значения и без привязки я не могу вставитьдата начала и дата окончания. У кого-нибудь есть какие-либо советы о том, как я могу сделать эту работу? Заранее спасибо

Я пытался создать представление, просто включив в запрос столбец даты, но не смогзаставить его работать после (запрос на просмотр)

SELECT

t.medico,
t.periodo, 
t.capacidademaxima, 
t.disponiveis, 
t.usados, 
t.faltas, 
t.cancelamentos, 
t.pa, 
t.reforcopa, 
t.compromissoparicular, 
t.outrosempregos, 
t.reunioeshdp, 
t.reunioeslaboratorios, 
t.almoco,
t.exameseprocedimentos, 
t.hospitaisecirurgias, 
t.aulas, 
t.congresso, 
t.ferias, 
t.maternidade, 
t.tratamentomedico

FROM (
  SELECT 
    obter_desc_agenda(cd_agenda) medico,

    'P1' periodo,

    count(case when a.ie_status_agenda <> 'C' then 1 else null end) capacidademaxima,

    count(case when nvl(a.nr_seq_motivo_transf,0) not in (30) and a.ie_status_agenda <> 'C' then 1 else null end) disponiveis,

    count(case when a.ie_status_agenda in ('E','L','N') then 1 else null end) usados,

    count(case when a.ie_status_agenda in ('I') then 1 else null end) faltas,

    count(case when a.ie_status_agenda in ('C') then 1 else null end) cancelamentos,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (10) then 1 else null end) pa,
    count(case when nvl(a.nr_seq_motivo_transf,0) in (15) then 1 else null end) reforcopa,
    count(case when nvl(a.nr_seq_motivo_transf,0) in (28) then 1 else null end)compromissoparicular,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (12) then 1 else null end) outrosempregos,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (16) then 1 else null end) reunioeshdp,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (26) then 1 else null end) reunioeslaboratorios,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (20) then 1 else null end) ferias,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (33) then 1 else null end) almoco,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (27) then 1 else null end) exameseprocedimentos,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (11) then 1 else null end) hospitaisecirurgias,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (13) then 1 else null end) aulas,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (14) then 1 else null end) congresso,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (32) then 1 else null end) tratamentomedico,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (31) then 1 else null end) maternidade

FROM
    agenda_consulta a 
WHERE
    trunc(a.dt_agenda, 'dd') between :dt_inicial and :dt_final 
    AND to_char(a.dt_agenda, 'HH24:MI:SS') between ('07:00:00') and ('08:59:59')
GROUP BY
    obter_desc_agenda(cd_agenda), 'P1'
UNION ALL
SELECT 
    obter_desc_agenda(cd_agenda) medico,

    'P2' periodo,

    count(case when a.ie_status_agenda <> 'C' then 1 else null end) capacidademaxima,

    count(case when nvl(a.nr_seq_motivo_transf,0) not in (30) and a.ie_status_agenda <> 'C' then 1 else null end) disponiveis,

    count(case when a.ie_status_agenda in ('E','L','N') then 1 else null end) usados,

    count(case when a.ie_status_agenda in ('I') then 1 else null end) faltas,

    count(case when a.ie_status_agenda in ('C') then 1 else null end) cancelamentos,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (10) then 1 else null end) pa,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (15) then 1 else null end) reforcopa,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (28) then 1 else null end)compromissoparicular,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (12) then 1 else null end) outrosempregos,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (16) then 1 else null end) reunioeshdp,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (26) then 1 else null end) reunioeslaboratorios,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (20) then 1 else null end) ferias,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (33) then 1 else null end) almoco,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (27) then 1 else null end) exameseprocedimentos,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (11) then 1 else null end) hospitaisecirurgias,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (13) then 1 else null end) aulas,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (14) then 1 else null end) congresso,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (32) then 1 else null end) tratamentomedico,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (31) then 1 else null end) maternidade
FROM
    agenda_consulta a 
WHERE trunc(a.dt_agenda, 'dd') between :dt_inicial and :dt_final  
    AND to_char (a.dt_agenda, 'HH24:MI:SS') between ('09:00:00') and ('11:59:59')
GROUP BY obter_desc_agenda(cd_agenda), 'P2'

UNION ALL

Then it repeats to P3, P4, P5 and the last part is "Total"

SELECT 

    obter_desc_agenda(cd_agenda) medico,

    'Total' periodo, 

    count(case when a.ie_status_agenda <> 'C' then 1 else null end) capacidademaxima,

    count(case when nvl(a.nr_seq_motivo_transf,0) not in (30) and a.ie_status_agenda <> 'C' then 1 else null end) disponiveis,

    count(case when a.ie_status_agenda in ('E','L','N') then 1 else null end) usados,

    count(case when a.ie_status_agenda in ('I') then 1 else null end) faltas,

    count(case when a.ie_status_agenda in ('C') then 1 else null end) cancelamentos,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (10) then 1 else null end) pa,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (15) then 1 else null end) reforcopa,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (28) then 1 else null end)compromissoparicular,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (12) then 1 else null end) outrosempregos,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (16) then 1 else null end) reunioeshdp,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (26) then 1 else null end) reunioeslaboratorios,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (20) then 1 else null end) ferias,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (33) then 1 else null end) almoco,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (27) then 1 else null end) exameseprocedimentos,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (11) then 1 else null end) hospitaisecirurgias,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (13) then 1 else null end) aulas,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (14) then 1 else null end) congresso,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (32) then 1 else null end) tratamentomedico,

    count(case when nvl(a.nr_seq_motivo_transf,0) in (31) then 1 else null end) maternidade

FROM
agenda_consulta a 

WHERE trunc(a.dt_agenda, 'dd') between :dt_inicial and :dt_final 

GROUP BY
    obter_desc_agenda(cd_agenda), 'Total'

) t

order by medico, periodo

Ответы [ 2 ]

0 голосов
/ 23 октября 2019

Создайте вид из этого выбора:

SELECT 
    obter_desc_agenda(cd_agenda) medico,
    case 
      when to_char(a.dt_agenda, 'HH24:MI:SS') between ('07:00:00') and ('08:59:59') then 'P1'
      when to_char(a.dt_agenda, 'HH24:MI:SS') between ('09:00:00') and ('11:59:59') then 'P2'
      when to_char(a.dt_agenda, 'HH24:MI:SS') between ('12:00:00') and ('14:59:59') then 'P3'
      when to_char(a.dt_agenda, 'HH24:MI:SS') between ('15:00:00') and ('16:59:59') then 'P4'
      when to_char(a.dt_agenda, 'HH24:MI:SS') between ('17:00:00') and ('19:59:59') then 'P5'
    end periodo,
    trunc(a.dt_agenda, 'dd') date_agenda,
    case when a.ie_status_agenda <> 'C' then 1 else null end capacidademaxima, -- don't count yet - only set flags (1 or null)
    case when nvl(a.nr_seq_motivo_transf,0) not in (30) and a.ie_status_agenda <> 'C' then 1 else null end disponiveis,
    case when a.ie_status_agenda in ('E','L','N') then 1 else null end usados,
    case when a.ie_status_agenda in ('I') then 1 else null end faltas,
    case when a.ie_status_agenda in ('C') then 1 else null end cancelamentos,
    case when nvl(a.nr_seq_motivo_transf,0) in (10) then 1 else null end pa,
    case when nvl(a.nr_seq_motivo_transf,0) in (15) then 1 else null end reforcopa,
    case when nvl(a.nr_seq_motivo_transf,0) in (28) then 1 else null end compromissoparicular,
    case when nvl(a.nr_seq_motivo_transf,0) in (12) then 1 else null end outrosempregos,
    case when nvl(a.nr_seq_motivo_transf,0) in (16) then 1 else null end reunioeshdp,
    case when nvl(a.nr_seq_motivo_transf,0) in (26) then 1 else null end reunioeslaboratorios,
    case when nvl(a.nr_seq_motivo_transf,0) in (20) then 1 else null end ferias,
    case when nvl(a.nr_seq_motivo_transf,0) in (33) then 1 else null end almoco,
    case when nvl(a.nr_seq_motivo_transf,0) in (27) then 1 else null end exameseprocedimentos,
    case when nvl(a.nr_seq_motivo_transf,0) in (11) then 1 else null end hospitaisecirurgias,
    case when nvl(a.nr_seq_motivo_transf,0) in (13) then 1 else null end aulas,
    case when nvl(a.nr_seq_motivo_transf,0) in (14) then 1 else null end congresso,
    case when nvl(a.nr_seq_motivo_transf,0) in (32) then 1 else null end tratamentomedico,
    case when nvl(a.nr_seq_motivo_transf,0) in (31) then 1 else null end maternidade
    from agenda_consulta a 

и используйте его так:

select medico, nvl(periodo, 'Total') -- nvl is there because grouping only by medico(counting Total) gives null in periodo
     , count(capacidademaxima)
     , count(disponiveis)
     , count(usados)
     ...
     , count(tratamentomedico)
     , count(maternidade)
  from viewName
 where date_agenda between :dt_inicial and :dt_final 
 group by grouping sets((periodo, medico), (medico)) -- it's the same as doing group by periodo, medico (counts groups P1 to P5) union all group by medico (counts Total)
0 голосов
/ 23 октября 2019

Возьмите trunc(a.dt_agenda, 'dd') до SELECT-list и GROUP BY-list и CREATE VIEW, как показано ниже:

CREATE [OR REPLACE] VIEW v_agenda_consulta AS
SELECT trunc(a.dt_agenda, 'dd') as dt_agenda, 'P1' as periodo, count( case when ....
  FROM agenda_consulta a 
 WHERE to_char(a.dt_agenda, 'HH24:MI:SS') between ('07:00:00') and ('08:59:59')
 GROUP BY trunc(a.dt_agenda, 'dd'), obter_desc_agenda(cd_agenda), 'P1'
UNION ALL
SELECT trunc(a.dt_agenda, 'dd'), 'P2' , count( case when ....
  FROM agenda_consulta a 
 WHERE to_char(a.dt_agenda, 'HH24:MI:SS') between ('07:00:00') and ('08:59:59')
 GROUP BY trunc(a.dt_agenda, 'dd'), obter_desc_agenda(cd_agenda), 'P2'
UNION ALL
SELECT trunc(a.dt_agenda, 'dd'), 'Total' , count( case when ....
  FROM agenda_consulta a 
 WHERE to_char(a.dt_agenda, 'HH24:MI:SS') between ('07:00:00') and ('08:59:59')
 GROUP BY trunc(a.dt_agenda, 'dd'), obter_desc_agenda(cd_agenda), 'Total';

И вы можете позвонить как

SELECT *
  FROM v_agenda_consulta 
 WHERE dt_agenda between :dt_inicial and :dt_final

Обратите вниманиепри первой попытке создать используйте CREATE VIEW v_agenda_consulta AS ... без опции REPLACE. Возможно, у вас есть существующий VIEW, который нужно переопределить.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...